While using selenium and xpath, I encountered a peculiar issue. On a page, there are 25 <a>
tags with nested <img/>
tags. I am trying to retrieve all these elements using the findElements()
method. Interestingly, upon inspecting the page source and searching for the substring "alt="Expand List"", I find 25 instances. However, when I run the command
let items = await driver.findElements(By.xpath("//a[//img[contains(@alt,'Expand List')]]"))
, it returns 32 items. Upon further investigation in Google Chrome's console, I noticed that some <a>
contain images with different alt values. Any insights on this issue? Here is the piece of code I used:
let text = await driver.getPageSource();
var count = (text.match(/alt="Expand List"/g) || []).length;
let items = await driver.findElements(By.xpath("//a[//img[contains(@alt,'Expand List')]]"))
console.log(count, items.length); //outputs 25, 32
An image with the alt attribute Collapse List
is also present within an <a>
tag:
https://i.sstatic.net/Suebu.png
Similarly, another image with the alt attribute Collapse List
appears within an <a>
tag: