xpath的Java Automation Selenium findElement有时找不到对象

问题描述

我对Selenium和自动化还很陌生。 我正在尝试对简单的健康声明表单页面进行自动化: elasticsearch

要进入文本字段,我使用了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

有什么建议吗?

解决方法

使用绝对xpath并不是一个好方法。我看不到应用程序中的xpath与代码中使用的xpath字符串匹配。尝试使用以下相对xpath:

WebElement element = driver.findElement(By.xpath("//input[@aria-labelledby='question1-title question1-required question1-questiontype']"));
element.sendKeys("Testing");

即使您可以使用唯一属性(例如)使xpath更短且唯一可识别。 //input[contains(@aria-labelledby,'question1-title')]

在文本框中输入文本之前无需执行点击操作。

,

请勿尝试自动执行Google表单 由于多种原因,不建议使用WebDriver登录Gmail和Facebook等网站。除了违反这些网站的使用条款(您可能会面临帐户被关闭的风险)之外,它的运行速度缓慢且不可靠。

理想的做法是使用电子邮件提供商提供的API,或者对于Facebook,使用开发人员工具服务,该服务公开用于创建测试帐户,朋友等的API。尽管使用API​​似乎有些额外的工作,但您会在速度,可靠性和稳定性上得到回报。该API也不太可能更改,而网页和HTML定位器经常更改,并且需要您更新测试框架。

在测试的任何时候使用WebDriver登录到第三方站点都会增加测试失败的风险,因为这会使您的测试时间更长。一般的经验法则是,更长的测试更加脆弱和不可靠。

符合W3C的WebDriver实施还使用WebDriver属性对导航器对象进行注释,以便可以缓解拒绝服务攻击。

,

由于该页面具有5个唯一的输入字段,为避免html代码更改和定位器的稳定性,可以直接使用以下定位器:

(//input)[1]

其中1是第一输入字段的索引。因此,您可以选择1-5中的任何值。

,

要在第一个元素中发送一个字符序列,并用占位符作为输入答案,您可以使用以下基于Locator Strategy

driver.findElement(By.xpath("//span[text()='Required']//following::div[1]/div//input[@placeholder='Enter your answer']")).sendKeys("LiorShor");

理想情况下,您需要为elementToBeClickable()引入WebDriverWait,并且可以使用以下解决方案:

new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Required']//following::div[1]/div//input[@placeholder='Enter your answer']"))).sendKeys("LiorShor");

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...