org.openqa.selenium.NoSuchElementException 导航至子项时 div Selenium Java
org.openqa.selenium.NoSuchElementException when navigating to child div Selenium Java
当前页面是https://www.nintendo.co.uk/Games/Games-347085.html
我已经 运行 使用 driver.findElement 对 div.row-page-container 进行了 Selenium 测试,它工作正常
但是当我试图通过进一步进入页面信息下面的红线来获取价格时,我找不到如何访问 class,它记录:org.openqa.selenium.NoSuchElementException : 没有这样的元素
如何获得价格值,同时尽可能避免使用 XPath?
不确定您为什么不想使用 XPath。
但是,如果您正在寻找基于游戏名称的 XPath,那么您可以获得它的价格。您可以使用下面的 XPath
//p[text()='Animal Crossing: New Horizons']//following-sibling::p[@class='price-small']//span[@data-price]
或下面的CSS也应该足够了
a[href*='Animal-Crossing-New-Horizons'] > p.price-small > span:nth-child(2)
基于hrefAnimal-Crossing-New-Horizons
首先使用正确的 xpath 是删除 NoSuchElementException.The 用于识别元素的 xpath 的重要步骤是
//p[text()='Animal Crossing: New Horizons']//following-sibling::p[@class='price-small']//span[@data-price]
即使无法识别,也可能是网页存在一些时间问题,即
页面仍在呈现,您已经完成了元素搜索并且未获得任何元素异常。
您可以使用 FluentWait 来克服这个问题。
假设您有一个元素,有时会在 1 秒内出现,有时需要几分钟才能出现。在这种情况下,最好使用 FluentWait 定义显式等待,因为这将尝试一次又一次地查找元素,直到找到它或直到最终计时器用完。
可以定义 FluentWait 的实例来配置等待条件的最长时间以及条件必须 checked.User 的频率也可以配置wait在等待元素时忽略特定类型的异常,比如页面上的NoSuchElementExceptions。
// 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);
添加 显式等待 也可以解决问题:
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();
当前页面是https://www.nintendo.co.uk/Games/Games-347085.html
我已经 运行 使用 driver.findElement 对 div.row-page-container 进行了 Selenium 测试,它工作正常
但是当我试图通过进一步进入页面信息下面的红线来获取价格时,我找不到如何访问 class,它记录:org.openqa.selenium.NoSuchElementException : 没有这样的元素
如何获得价格值,同时尽可能避免使用 XPath?
不确定您为什么不想使用 XPath。
但是,如果您正在寻找基于游戏名称的 XPath,那么您可以获得它的价格。您可以使用下面的 XPath
//p[text()='Animal Crossing: New Horizons']//following-sibling::p[@class='price-small']//span[@data-price]
或下面的CSS也应该足够了
a[href*='Animal-Crossing-New-Horizons'] > p.price-small > span:nth-child(2)
基于hrefAnimal-Crossing-New-Horizons
首先使用正确的 xpath 是删除 NoSuchElementException.The 用于识别元素的 xpath 的重要步骤是
//p[text()='Animal Crossing: New Horizons']//following-sibling::p[@class='price-small']//span[@data-price]
即使无法识别,也可能是网页存在一些时间问题,即 页面仍在呈现,您已经完成了元素搜索并且未获得任何元素异常。
您可以使用 FluentWait 来克服这个问题。
假设您有一个元素,有时会在 1 秒内出现,有时需要几分钟才能出现。在这种情况下,最好使用 FluentWait 定义显式等待,因为这将尝试一次又一次地查找元素,直到找到它或直到最终计时器用完。
可以定义 FluentWait 的实例来配置等待条件的最长时间以及条件必须 checked.User 的频率也可以配置wait在等待元素时忽略特定类型的异常,比如页面上的NoSuchElementExceptions。
// 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);
添加 显式等待 也可以解决问题:
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();