Skip to content
Advertisement

XPath doesn’t locate element

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"]

Advertisement

Answer

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']")));
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement