Selenium webdriver cannot identify links on a page using a tag

Selenium displays the number of links on the page as 0, although there are many links on the page.

This is my code in java

dr.get("https://www.ebay.com");
List<WebElement> linksize = dr.findElements(By.tagName("a"));
System.out.println(linksize.size());

Output: 0

0
source share
2 answers

Wait for the page to load links

Change your code to

dr.get("https://www.ebay.com");
waitForLoad(dr); // Here you are calling the below method
List<WebElement> linksize = dr.findElements(By.tagName("a"));
System.out.println(linksize.size());

You can use the method below as your resource and you can call anytime.

void waitForLoad(WebDriver driver) {
    ExpectedCondition<Boolean> pageLoadCondition = new
        ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
        };
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(pageLoadCondition);
}
+1
source

@Subhrapratim Bhattacharjee, it seems that you need to wait for the page to load. Try the following code

WebDriver driver = new FirefoxDriver();
    driver.get("https://www.ebay.com");
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    WebDriverWait wait = new WebDriverWait(driver, 30);
    List<WebElement> linksize = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName("a")));//driver.findElements(By.tagName("a"));
    System.out.println(linksize.size());
0
source

Source: https://habr.com/ru/post/1656034/


All Articles