简体   繁体   English

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

[英]Unable to enter text to input type on Webpage using Selenium Java

Uploaded my entire webpage on which my test is failing here: https://drive.google.com/file/d/1WHcwpQFi5Cxh1q1MupQEuSPk6CPZs2GC/view?usp=sharing在此处上传了我的测试失败的整个网页: https://drive.google.com/file/d/1WHcwpQFi5Cxh1q1MupQEuSPk6CPZs2GC/view?usp=sharing

Below is my Selenium Java code auto-generated by Selenium Recorder IDE Plugin where I'm trying to enter text to input-type on my webpage. Below is my Selenium Java code auto-generated by Selenium Recorder IDE Plugin where I'm trying to enter text to input-type on my webpage.

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");

The above did not work and errors.以上没有工作和错误。 Hence, I made the below changes to my code:因此,我对我的代码进行了以下更改:

The below code works intermittently but rarely does it work.下面的代码间歇性地工作,但很少工作。

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());

I get the below error running my test:运行测试时出现以下错误:

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)

Here is the relevant view source code for the text input from the web page:以下是从web页面输入的文本的相关视图源代码:

<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"> 

Issue trying @Norayr Sargsyan solution:尝试@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)

Issue trying @Norayr Sargsyan second solution with for loop & Actions class使用 for 循环和Actions class 尝试 @Norayr Sargsyan 的第二个解决方案时出现问题

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)

Issue trying @DebanjanB solution:尝试@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)

Can you please suggest how can I get it to work?你能建议我如何让它工作吗?

I think the element which you needed is present on the DOM, but it is not in a state that you can interact with them.我认为您需要的元素存在于 DOM 中,但它不在您可以与它们交互的 state 中。

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");

or或者

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());
   }
}

Instead of invoking sendKeys() to the <label> you need to invoke on the <input> element and ideally to click() on the element you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies :而不是调用sendKeys()<label>您需要在<input>元素上调用,理想情况下是click()您需要对elementToBeClickable()使用WebDriverWait的元素,您可以使用以下任一定位器策略

  • cssSelector : 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 : 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");

References参考

You can find a couple of relevant discussions on ElementNotInteractableException in:您可以在以下位置找到有关ElementNotInteractableException的一些相关讨论:

I have been trying past 24 hours to get this to work and finally did.过去 24 小时我一直在努力让它发挥作用,最终做到了。

Before, I trigger the Search Assests button as visible in my video @ 1.11 minutes I tried the below code which worked.之前,我触发了我的视频中可见的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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium - 使用 java 脚本输入文本 - Selenium - Enter a text using java script 无法在Selenium Java的电子邮件正文中键入文本 - Unable to type text in email body for Selenium Java 无法在 Java 中正确输入 Input - Unable to enter Input properly in Java 如何在网页上使用带有 Java 的 Selenium WebDriver 验证工具提示文本 - How to verify tooltip text using Selenium WebDriver with java on a webpage 如何使用 Selenium 和 Java 在 https://www.phptravels.net/home 的聊天机器人表单输入字段中输入文本 - How to enter text with in the chat bot form input fields of https://www.phptravels.net/home using Selenium and Java 如何使用Selenium Webdriver在具有onblur,onfocus和onkeydown属性的网页文本框中输入文本? - How to enter a text in a textbox in a webpage with onblur,onfocus and onkeydown attributes using Selenium Webdriver? 我无法在动态文本字段中输入逻辑代码来输入引脚号。使用Selenium Webdriver Java - I am unable to enter logic code to enter pin number in dynamic textfield.Using Selenium Webdriver Java 无法在Selenium Java中选择输入文本框元素 - Unable to select input text box element in selenium java 如果元素类型是,如何使用 Selenium(对于 Java)定位输入文本框<object></object> - How to target input text box using Selenium (for Java) if type of element is <object></object> 使用java从文本文件将信息输入到网页 - Using java to input information onto a webpage from a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM