简体   繁体   中英

org.openqa.selenium.NoSuchElementException when navigating to child div Selenium Java

The current page is https://www.nintendo.co.uk/Games/Games-347085.html

I've run a Selenium test with driver.findElement for div.row-page-container and it works fine

but when I try to get the price value by going further into the red line below for the page-info I can't find how to access the class, it logs: org.openqa.selenium.NoSuchElementException: no such element

How can I get the price value, while avoiding XPath if possible?

Not sure why you do not want to have XPath.

But in case if you are looking for XPath based on Game name, that can get you it's price. You can use the below XPath

//p[text()='Animal Crossing: New Horizons']//following-sibling::p[@class='price-small']//span[@data-price]

or the below CSS should suffice as well

a[href*='Animal-Crossing-New-Horizons'] > p.price-small > span:nth-child(2)

It is based on href Animal-Crossing-New-Horizons

First of all using the correct xpath is an important step to remove the the NoSuchElementException.The xpath for identifying the element is

//p[text()='Animal Crossing: New Horizons']//following-sibling::p[@class='price-small']//span[@data-price]

Even if this doesn't identify then there might be some timing issues with the webpage ie the page is still being rendered and you've already finished your element search and obtained no element exception.

You can use FluentWait to overcome this problem.

Let's say you have an element which sometimes appears in just 1 second and sometimes it takes minutes to appear. In that case it is better to define an explicit wait using FluentWait , as this will try to find an element again and again until it finds it or until the final timer runs out.

An instance of FluentWait can be defined to configure the maximum amount of time to wait for a condition as well as the frequency at which the condition must be checked.User can also configure the wait to ignore specific types of exceptions while waiting for an element, such as NoSuchElementExceptions on the page.

 // Waiting 30 seconds for an element to be present on the page, checking 
// for its presence once every 5 seconds. 
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
                                           .withTimeout(30, SECONDS);
                                           .pollingEvery(5, SECONDS) ;
                                           .ignoring(NoSuchElementException.class);
    

Adding explicit wait might also do the trick:

import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
 . . . 
WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element_xpath"))); element.click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM