简体   繁体   English

org.openqa.selenium.NoSuchElementException 导航到子 div Selenium Java

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

The current page is https://www.nintendo.co.uk/Games/Games-347085.html当前页面是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我已经使用 driver.findElement 为 div.row-page-container 运行了 Selenium 测试,它工作正常

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但是当我尝试通过进一步进入页面信息下面的红线来获取价格值时,我找不到如何访问 class,它会记录:org.openqa.selenium.NoSuchElementException:没有这样的元素

How can I get the price value, while avoiding XPath if possible?如何获得价格价值,同时尽可能避免 XPath?

Not sure why you do not want to have XPath.不知道为什么你不想拥有 XPath。

But in case if you are looking for XPath based on Game name, that can get you it's price.但是,如果您正在寻找基于游戏名称的 XPath,那可以得到它的价格。 You can use the below XPath您可以使用下面的 XPath

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

or the below CSS should suffice as well或下面的 CSS 也应该足够了

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

It is based on href Animal-Crossing-New-Horizons它基于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首先使用正确的 xpath 是去除 NoSuchElementException 的重要步骤。用于识别元素的 xpath 是

//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.您可以使用FluentWait来克服这个问题。

Let's say you have an element which sometimes appears in just 1 second and sometimes it takes minutes to appear.假设您有一个元素,有时只需 1 秒即可出现,有时需要几分钟才能出现。 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.在这种情况下,最好使用FluentWait定义显式等待,因为这将一次又一次地尝试查找元素,直到找到它或直到最终计时器用完。

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.可以定义FluentWait的实例来配置等待条件的最长时间以及必须检查条件的频率。用户还可以配置等待以在等待元素时忽略特定类型的异常,比如页面上的 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);
    

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.openqa.selenium.NoSuchElementException WebDriver是否在Java中? - org.openqa.selenium.NoSuchElementException WebDriver in Java? org.openqa.selenium.NoSuchElementException - org.openqa.selenium.NoSuchElementException WebdriverIO 和 Java - IFrame 中的 org.openqa.selenium.NoSuchElementException - WebdriverIO & Java - org.openqa.selenium.NoSuchElementException in IFrame org.openqa.selenium.NoSuchElementException即使存在该元素 - org.openqa.selenium.NoSuchElementException even when the element is present 如何解决org.openqa.selenium.NoSuchElementException - How to solve the org.openqa.selenium.NoSuchElementException 当我单击子窗口中的按钮时,收到“ org.openqa.selenium.NoSuchElementException:没有这样的元素” - “org.openqa.selenium.NoSuchElementException: no such element” received when i click button in child window 如何解决 Selenium 中的 org.openqa.selenium.NoSuchElementException? - How to resolve org.openqa.selenium.NoSuchElementException in Selenium? org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element error using Selenium through Java - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element error using Selenium through Java org.openqa.selenium.NoSuchElementException:没有这样的元素:尝试使用Java通过Selenium登录时无法找到元素 - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element while trying to login through Selenium with Java IE11的org.openqa.selenium.NoSuchElementException - org.openqa.selenium.NoSuchElementException for IE11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM