Ashar
Ashar

Reputation: 3245

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

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

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)

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)

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?

Upvotes: 0

Views: 1004

Answers (3)

Ashar
Ashar

Reputation: 3245

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

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

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193078

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:

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

References

You can find a couple of relevant discussions on ElementNotInteractableException in:

Upvotes: 0

Norayr Sargsyan
Norayr Sargsyan

Reputation: 1868

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.

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

Upvotes: 0

Related Questions