无法使用Selenium Java在网页上输入文本作为输入类型 [英] Unable to enter text to input type on Webpage using Selenium Java

查看:33
本文介绍了无法使用Selenium Java在网页上输入文本作为输入类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里上传了我的测试失败的整个网页: https://drive.google.com/file/d/1WHcwpQFi5Cxh1q1MupQEuSPk6CPZs2GC/view?usp = sharing

Uploaded my entire webpage on which my test is failing here: https://drive.google.com/file/d/1WHcwpQFi5Cxh1q1MupQEuSPk6CPZs2GC/view?usp=sharing

下面是Selenium Recorder IDE插件自动生成的Selenium Java代码,在这里我试图在网页上输入文本作为输入类型.

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:

<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解决方案:

Issue trying @Norayr Sargsyan solution:

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)

尝试使用for循环&的@Norayr Sargsyan第二种解决方案 Actions

Issue trying @Norayr Sargsyan second solution with for loop & Actions class

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解决方案的问题:

Issue trying @DebanjanB solution:

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?

推荐答案

我已经尝试了24小时,以使其正常工作,并最终做到了.

I have been trying past 24 hours to get this to work and finally did.

在此之前,我在尝试以下代码的1.11分钟后触发了 Search Assests 按钮,该按钮在我的视频中显示为可见.

Before, I trigger the Search Assests button as visible in my video @ 1.11 minutes I tried the below code which worked.

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

这篇关于无法使用Selenium Java在网页上输入文本作为输入类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆