Java 有时找不到Xpth元素 这是我的错误
不可用 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 不可用Java 有时找不到Xpth元素 这是我的错误,java,debugging,selenium-webdriver,xpath,Java,Debugging,Selenium Webdriver,Xpath,不可用 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 无法使用的 不可用 Nov 23, 2018 10:22:08 AM org.openqa.selenium.remote.DesiredCapabilities firefox INFO: Using `new FirefoxOptions()` is preferred to `DesiredCapabilities.firefox()` Starting ChromeDriver 2.4
Nov 23, 2018 10:22:08 AM org.openqa.selenium.remote.DesiredCapabilities firefox
INFO: Using `new FirefoxOptions()` is preferred to `DesiredCapabilities.firefox()`
Starting ChromeDriver 2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a) on port 11629
Only local connections are allowed.
Nov 23, 2018 10:22:13 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
这行包含
位于automation.ExampleScanario.main ExampleScanario.java:52
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//label[@class='checkbox ng-binding']//input[@type='checkbox']"}
*** Element info: {Using=xpath, value=//label[@class='checkbox ng-binding']//input[@type='checkbox']}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at automation.ExampleScanario.main(ExampleScanario.java:52)
在自动化测试中,XPath是我用来定位元素的最后一个选项。XPath只不过是元素的一个可能的唯一路径,它包含一系列用于形成唯一路径的标记。它可以随时更改
我建议您在测试运行时测试XPath。在使用XPath和再次检查之前添加睡眠时间。这次您肯定会得到一个不同的路径。而不是Thread.sleep750;您应该使用显式wait for same。 这里750不到1秒,网页需要一些时间来加载,我们不能用特定的时间来定义它
driver.findElement(By.xpath("//label[@class='checkbox ng-binding']//input[@type='checkbox']")).click();
driver.findElement(By.xpath("//label[@class='checkbox ng-binding']//input[@type='checkbox']")).click();
WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("Your Element")).click());