Reputation: 31
I am trying to find list of all anchor elements, which I got during search result.
When I try by CSS:
List<WebElement> anchors = driver.findElements(By.cssSelector("ul a"));
I am getting many results.
When I try by XPATH:
By.xpath("/html/body/div[2]/div[1]/header/div/form/div[2]/div/div[4]/ul/a[1]
I'm getting only one.
I need all of them.
Upvotes: 3
Views: 6115
Reputation: 2115
First get all anchor elements and keep them on a WebElement
list. Then find the link address using getAttribute("href")
in the loop. You can use like below:
List<WebElement> allAnchors = driver.findElements(By.cssSelector("div.clerk-live-search-container > div > ul > a"));
for (WebElement eachAnchorElem : allAnchors) {
String link = eachAnchorElem.getAttribute("href");
//System.out.println(link);
}
You can use to keep the link adrress in the String array
or list
Upvotes: 0
Reputation: 193088
Let us analyze what happened with your code trials :
When you tried :
List<WebElement> anchors = driver.findElements(By.cssSelector("ul a"));
You got back 133 matching nodes because where ever an <a>
tag followed by <ul>
was found that was added to the List
. We don't need that actually.
When you tried :
By.xpath("/html/body/div[2]/div[1]/header/div/form/div[2]/div/div[4]/ul/a[1]
As the xpath
is an absolute xpath
with an index [1]
you get back only one element.
Once you key in the text cetaphi
within the Search Box
you will find 7 search results within the View Port
. To get the list of all anchor elements within the Search Results
you can use the following code block :
List<WebElement> anchors = driver.findElements(By.xpath("//div[@class='clerk-livesearch-title' and contains(text(),'Products')]//following::a/div[@class='clerk-live-item']//div/p"));
for (WebElement ele:anchors)
System.out.println(ele.getAttribute("innerHTML"));
Note : There are certain other
div
andul
withid
attribute set as__clerk-1-442699327
,__clerk-__clerk-13-460558980
etc and they seems to be dynamic. Hence those must be avoided.
Upvotes: 1
Reputation: 11
Looks like all anchor elements have the href start with "/products...", if so, please try this xpath:
By.xpath("//a[starts-with(@href, '/products')]")
Upvotes: 1
Reputation: 25587
So what you want is to get all the links in the suggestions list. I'm assuming you want to ignore the CATEGORIES link(s) and only get link to products.
There are several ways you can get this. One way is to find the UL
that has an ID... it's the only one (currently) on the page. You can use the CSS selector below. This is looking for all A
s that are direct descendants to any UL
that contains any ID.
ul[id] > a
Another way, and probably safer, is to not rely on the fact that the UL
is the only UL
on the page that has an ID. We can find these elements using the container for the suggestions dropdown and then find the A
s in the UL
. The DIV
that is the container for the suggestions dropdown is
<div class="clerk-live-search-container" id="__clerk-2-318441846" style="...">
That ID changes each time so we can't rely on it. There are also a number of other IDs on the page that start with __clerk
so we can't use that either. This DIV
does have a unique class on it, class="clerk-live-search-container"
, so we can use that. We can start with this DIV
and then find the UL
that contains all the A
s we want. The CSS selector is below.
div.clerk-live-search-container ul > a
Both selectors above work but I would probably use the 2nd one because I think it's less likely to break in the future.
Upvotes: 0