开发者问题收集

无法使用 Selenium Java 在网页上输入文本

2020-07-07
928

已将我的整个网页上传,但测试失败: https://drive.google.com/file/d/1WHcwpQFi5Cxh1q1MupQEuSPk6CPZs2GC/view?usp=sharing

下面是 Selenium Recorder IDE 插件自动生成的 Selenium Java 代码,我试图在网页上输入文本。

driver.findElement(By.id("crtestrequest-cr_app_name")).click();
driver.findElement(By.id("crtestrequest-cr_app_name")).clear();
driver.findElement(By.id("crtestrequest-cr_app_name")).sendKeys("Allocation");

以上操作无效,并出现错误。因此,我对代码进行了以下更改:

以下代码间歇性地工作,但很少工作。

js.executeScript("arguments[0].click();", driver.findElement(By.id("crtestrequest-cr_app_name")));
System.out.println("Title of the page is 7.19 -> " + driver.getTitle());
driver.findElement(By.id("crtestrequest-cr_app_name")).clear();
System.out.println("Title of the page is 7.20 -> " + driver.getTitle());
driver.findElement(By.id("crtestrequest-cr_app_name")).sendKeys("Allocation");
System.out.println("Title of the page is 7.21 -> " + driver.getTitle());

我在运行测试时收到以下错误:

Title of the page is 7.18 -> Raise CR Request
Title of the page is 7.19 -> Raise CR Request
Title of the page is 7.20 -> Raise CR Request
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
  (Session info: chrome=75.0.3770.100)
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'myhost', ip: '10.9.140.15', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Usersxmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:62388}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 5d2f84a25fb9daaaf3c5360a3f6c17e9
        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.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.RemoteWebElement.execute(RemoteWebElement.java:285)
        at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
        at pack.SynvmRequest.testSynvmRequest(SynvmRequest.java:301)
        at pack.SynvmRequest.main(SynvmRequest.java:538)

这是来自网页的文本输入的相关查看源代码:

<div class="row"><div class="col-lg-12"></div></div>
<div class="row"><div class="col-lg-12"></div></div>

<div class="row"><div class="col-lg-12"><div class="form-group col-lg-6 field-crtestrequest-cr_app_name required">

<label class="control-label" for="crtestrequest-cr_app_name">App Name as per AOP</label>

<input type="text" id="crtestrequest-cr_app_name" class="form-control" name="CRTestRequest[cr_app_name]" value="" maxlength="150" aria-required="true"> 

尝试@Norayr Sargsyan解决方案时出现问题:

Title of the page is 7.19 -> Raise CR Request
Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label class="control-label" for="crtestrequest-cr_app_name">...</label> is not clickable at point (206, 500). Other element would receive the click: <div class="panel-body" style="margin-top:-21px;">...</div>
  (Session info: chrome=75.0.3770.100)
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'myhost', ip: '10.9.140.15', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Usersxmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:63030}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 4791be3194b3fa8635c5b82962202579
        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.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.RemoteWebElement.execute(RemoteWebElement.java:285)
        at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
        at pack.SynvmRequest.testSynvmRequest(SynvmRequest.java:301)
        at pack.SynvmRequest.main(SynvmRequest.java:542)

尝试@Norayr Sargsyan第二个解决方案时出现问题,其中包含for循环和 Actions

Title of the page is 7.19 -> Raise CR Request
Trying to recover from a stale element :stale element reference: element is not attached to the page document
  (Session info: chrome=75.0.3770.100)
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'myhost', ip: '10.9.140.15', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Usersxmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:64331}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 1c190d50b7d77839ad4d4d4452576ad3
Title of the page is 7.19 -> Raise CR Request
Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <input type="text" id="crtestrequest-cr_app_name" class="form-control" name="CRTestRequest[cr_app_name]" value="" maxlength="150" aria-required="true"> is not clickable at point (533, 496). Other element would receive the click: <footer class="main-footer main-content clearfix">...</footer>
  (Session info: chrome=75.0.3770.100)
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'myhost', ip: '10.9.140.15', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Usersxmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:64331}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 1c190d50b7d77839ad4d4d4452576ad3
        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.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.RemoteWebElement.execute(RemoteWebElement.java:285)
        at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
        at pack.SynvmRequest.testSynvmRequest(SynvmRequest.java:311)
        at pack.SynvmRequest.main(SynvmRequest.java:576)

尝试@DebanjanB解决方案时出现问题:

Title of the page is 7.19 -> Raise CR Request
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=75.0.3770.100)
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'myhost', ip: '10.9.140.15', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Usersxmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:63253}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 4699d09a1abe8089612d70509d5047e4
        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.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.RemoteWebElement.execute(RemoteWebElement.java:285)
        at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
        at pack.SynvmRequest.testSynvmRequest(SynvmRequest.java:307)
        at pack.SynvmRequest.main(SynvmRequest.java:551)

您能建议我如何让它工作吗?

3个回答

我在过去 24 小时内一直在尝试让它工作,最后终于成功了。

之前,我触发了 Search Assests 按钮,如我的视频中所示,@1.11 分钟我尝试了下面的代码,它起作用了。

WebElement ele = driver.findElement(By.id("crtestrequest-cr_app_code"));
JavascriptExecutor exec = (JavascriptExecutor)driver;
exec.executeScript("arguments[0].click();", ele);
driver.findElement(By.id("crtestrequest-cr_app_code")).clear();
driver.findElement(By.id("crtestrequest-cr_app_code")).sendKeys("Allocation");
Ashar
2020-07-08

我认为您需要的元素存在于 DOM 上,但它不处于您可以与它们交互的状态。

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.id("crtestrequest-cr_app_name")));
WebLement input = driver.findElement(By.id("crtestrequest-cr_app_name"))
input.click();
input.clear();
input.sendKeys("123");

for(int i=0; i<=2;i++){
  try{
     WebLement input = driver.findElement(By.id("crtestrequest-cr_app_name"))
     Actions actions = new Actions(driver);
     actions.moveToElement(input).perform();
     input.click();
     break;
   } catch (StaleElementReferenceException e) {
           System.out.println("Trying to recover from a stale element :" + e.getMessage());
   }
}
Norayr Sargsyan
2020-07-07

您不需要对 <label> 调用 sendKeys() ,而是需要在 <input> 元素上调用,最好对元素调用 click() ,您需要对 elementToBeClickable() 使用 WebDriverWait ,并且可以使用以下任一方法 定位器策略

  • cssSelector

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.row div.field-crtestrequest-cr_app_name>label[for='crtestrequest-cr_app_name']")));
    element.click();
    element.clear();
    element.sendKeys("Allocation");
    
  • xpath :

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='row']//div[contains(@class, 'field-crtestrequest-cr_app_name')]/label[@for='crtestrequest-cr_app_name']")));
    element.click();
    element.clear();
    element.sendKeys("Allocation");
    

参考资料

您可以在以下位置找到有关 ElementNotInteractableException 的几个相关讨论:

undetected Selenium
2020-07-07