ngk123
ngk123

Reputation: 31

Get list of all elements from search result in Selenium Webdriver

enter image description here

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

Answers (5)

Ali Azam
Ali Azam

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

undetected Selenium
undetected Selenium

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.

Solution :

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 and ul with id attribute set as __clerk-1-442699327, __clerk-__clerk-13-460558980 etc and they seems to be dynamic. Hence those must be avoided.

Upvotes: 1

jaibalaji
jaibalaji

Reputation: 3475

You can also use:

By.xpath(".//a[contains(@href,'products')]");

Upvotes: 0

Shawn
Shawn

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

JeffC
JeffC

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 As 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 As 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 As 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

Related Questions