我对 Selenium 和自动化还很陌生。我正在尝试为简单的健康声明表单页面进行自动化: https ://forms.office.com/Pages/ResponsePage.aspx?id=bGOiBG0y_0iT-HCYdb06qZZ8CdlEQAhOkRllU1E9dVZUMVk1VTZFWThQV1FQUTFUV0FKNkNOVldMSi4u
要进入我使用 xpath 的文本字段:
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://forms.office.com/Pages/ResponsePage.aspx?id=bGOiBG0y_0iT-HCYdb06qZZ8CdlEQAhOkRllU1E9dVZUMVk1VTZFWThQV1FQUTFUV0FKNkNOVldMSi4u");
WebElement element = driver.findElement(By.xpath("//*[@id=\"form-container\"]/div/div/div[1]/div/div[1]/div[2]/div[2]/div[1]/div[2]/div[3]/div/div/div/input"));
element.click();
element.sendKeys("Testing");
问题是有时它找不到元素并且程序崩溃。
*** Element info: {Using=xpath, value=//*[@id="form-container"]/div/div/div[1]/div/div[1]/div[2]/div[2]/div[1]/div[2]/div[3]/div/div/div/input}
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
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 automactionproj.MainActivity.hazaratBriyot(MainActivity.java:31)
at automactionproj.MainActivity.main(MainActivity.java:23)
Process finished with exit code 1
有什么建议么?