0

Below code is not able to identify the list of webelements if they are identified inside a wait condition. I get an exception as timeoutexception and unable to identify the element for the specified xpath.

However if I directly access the elements without wait condition , the values are assigned to the list Variable, why is this so?

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.finance.yahoo.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[@id='yfin-usr-qry']")).sendKeys("nclh");

WebDriverWait wait = new WebDriverWait(driver,5);
List<WebElement>dd_list= wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@class='modules_list__1zFHY']/li")));
System.out.println(dd_list.size());

for(WebElement ele : dd_list) {

    if (ele.getText().contains("NCLH.VI")) {
        System.out.println("i got the element");
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

why is this so?

The elements that you are targeting are never all visible simultaneously. Your xpath returns 15 elements, of which only 10 are visible. Implying that your condition will never be met (hence the timeout exception). Simply refine your xpath so as to target the elements you are interested in: the ones that have vocation to be visible, e.g. "//ul[@class='modules_list__1zFHY']/li[@data-type='quotes']"

enter image description here

keepAlive
  • 6,369
  • 5
  • 24
  • 39
0

The Yahoo Finance website contains ReactJS enabled elements. So you need to induce WebDriverWait for document.readyState to be complete and you can use the following Locator Strategies:

  • Code Block:

    driver.get("https://www.finance.yahoo.com");
    new WebDriverWait(driver, 120).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='yfin-usr-qry']")));
    element.click();
    element.sendKeys("nclh");
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@class='modules_list__1zFHY']//li[@data-type='quotes']"))).size())
    
  • Console Output:

    6
    
  • Browser Snapshot:

YahooFinance

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352