java - 有时找不到 Xpath 元素

标签 java debugging selenium-webdriver xpath

Boolean run = true;

        while(run)
        {
            if (nomore.contentEquals(tt))
                {
                driver.navigate().to("https://mettl.com/corporate/live-feed#/proctoringDashboard");
                Thread.sleep(750);
                driver.findElement(By.xpath("//label[@class='checkbox ng-binding']//input[@type='checkbox']")).click();
                driver.findElement(By.xpath("//button[contains(text(),'Authorize')]")).click();
                //Thread.sleep(1200);
                System.out.println("Not Available");

                }

            else
                {

                //Thread.sleep(10000);
                run = false;
                System.out.println("Available");
                break;                                  
                } 

        } 

这是我的错误

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

不可用 无法使用 无法使用 无法使用 无法使用 无法使用 无法使用 无法使用 无法使用 无法使用 不可用

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)

这一行包含 在 Automation.ExampleScanario.main(ExampleScanario.java:52)

driver.findElement(By.xpath("//label[@class='checkbox ng-binding']//input[@type='checkbox']")).click();

最佳答案

代替 Thread.sleep(750);您应该使用显式等待。 这里的750是不到1秒,网页需要一些时间来加载,我们无法定义具体的时间。

WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("Your Element")).click());

关于java - 有时找不到 Xpath 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53440857/

相关文章:

java - 从静态方法访问非静态成员变量

java - JFrame/JPanel 中的 GridLayout 被挤压在一起

java - 转储执行 - java?

java - 运行使用外部 jar 文件的 java 文件

java - 如何将字符串转换为双[][]

debugging - Android NDK调试: device can't be opened

Android/Eclipse : where can I find the/Users/USER_NAME/. android/debug.keystore 文件夹?

java - 使用 JUnit 和 Selenium WebDriver 时浏览器未启动

javascript - Angular + Selenium : Cannot get value from input field

python - 在selenium python中每5秒打印一次移动文本