gpt4 book ai didi

java - 有时找不到 Xpath 元素

转载 作者:行者123 更新时间:2023-12-02 10:33:56 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com