Home > Software design >  XPath doesn't locate element
XPath doesn't locate element

Time:03-05

I want to scrape links from this page. https://www.youtube.com/results?search_query=food recepies

WebElement w1 = driver.findElement(By.xpath("//a[@id=\"video-title\"]"));
WebElement w2 = driver.findElement(By.xpath("//*a[@id='video-title']"));
WebElement w3 = driver.findElement(By.xpath("//a[@class='yt-simple-endpoint style-scope ytd-video-renderer']"));
WebElement w4 = driver.findElement(By.xpath("//h3/a"));

String link = w1.getAttribute("href");

None of these works for me.

Error: Unable to locate element: //a[@id="video-title"]

CodePudding user response:

This should work for the title.

driver.findElement(By.xpath("//a[@id='video-title']"));

However, there are 27 of them. So, you might want to use

List<WebElement> plants = driver.findElements(By.xpath("//a[@id='video-title']"));

Another thing is that you might want to wait until the elements are visible, enabled, clickable, etc., on the page. For this, you need to do something like the following:

WebElement firstResult = new WebDriverWait(driver, Duration.ofSeconds(10))
     .until(ExpectedConditions.elementToBeVisible(By.xpath("//a[@id='video-title']")));
  • Related