Selenium Error "Element is not currently interactable and may not be manipulated"

Selenium webdriver can drive different browsers like as Firefox, Chrome or Internet Explorer. These browsers actually cover the majority of internet users, so testing these browsers possibly covers the 90% of the internet users. However, there is no guaranty that the same automation scripts can work without a failure on these three browsers. For this reason, automation code should be error-prone for the browsers you want to cover.

The following error is caught when the test script run for Chrome and Internet Explorer, but surprisingly there is no error for the Firefox. Selenium gives an error like below:
Traceback (most recent call last):
  File "D:\workspace\sample_project\sample_run.py", line 10, in <module>
    m.login()
  File "D:\workspace\sample_project\test_case_imps.py", line 335, in login
    driver.find_element_by_id("id_username").clear()
  File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 62, in clear
    self._execute(Command.CLEAR_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 228, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidElementStateException: Message: u'invalid element state: Element is not currently interactable and may not be manipulated\n  (Session info: chrome=29.0.1547.76)\n  (Driver info: chromedriver=2.3,platform=Windows NT 6.2 x86_64)'

In this example, while Firefox can find the element, however Chrome and Internet Explorer can't find the element. This is because the Firefox can load the page little bit faster than the other, and when the other browsers look up the element, it not loaded yet. To solve the problem we can let the browser wait until the browsers can load the element. time.sleep(1)  adds the following piece of code wait for 1 second before executing the line 335. Similarly, driver.implicitly_wait(1) can be used.

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()

driver.find_element_by_css_selector("span.ml5.dib").click()
time.sleep(1) 
driver.find_element_by_id("id_username").clear()
driver.find_element_by_id("id_username").send_keys("tst@test.com")



A better approach is to wait until a dynamic object in the opening page is visible which means that the page is loaded correctly. Then we can click the object. The loading time can be different because of the browser and the connection speed. Therefore this approach can solve other possibilities. You can use WebDriverWait, to use this you must import this library first as in the example.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

driver = webdriver.Firefox()

driver.find_element_by_css_selector("span.ml5.dib").click()

# wait until a dynamic object to be visible 
# for as much as timeout, as 10 second here
try:
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))
finally:
    driver.quit()

driver.find_element_by_id("id_username").clear()
driver.find_element_by_id("id_username").send_keys("tst@test.com")

Comments

  1. I am using Selenium with PhantomJS and this didn't work. Any other ideas? It is clearly enabled as I can see it in the screenshot I made.

    ReplyDelete
    Replies
    1. If waiting the object until to be loaded cant work for your script, there should be a problem with finding object.

      You could change the method of finding element for example you can use driver.find_element_by_xpath() instead of find_element_by_id.

      Delete
  2. I came across this when using robot framework with chrome.
    This come in randomly ( Exactly the element has not loaded properly when the command on the element is executed)

    KEYWORD: Selenium2Library.Capture Page Screenshot Documentation: Takes a screenshot of the current page and embeds it into the log.

    Start / End / Elapsed: 20140326 14:25:20.975 / 20140326 14:25:21.242 / 00:00:00.267

    14:25:20.878 INFO Typing text 'sampleOneUVersion' into text field 'xpath=//*[@id='ng-app']/body//input[@ng-model='search']'

    14:25:21.244 FAIL InvalidElementStateException: Message: u'invalid element state: Element is not currently interactable and may not be manipulated\n (Session info: chrome=33.0.1750.154)\n (Driver info: chromedriver=2.8.241075,platform=Windows NT 6.1 SP1 x86_64)'

    ReplyDelete
    Replies
    1. It is the same problem. Adding time.sleep(time_to_wait) can solve the problem but loading time of the object depends on many factors such as network speed, browser, the load on the server (that's why sometimes you don't see the problem.)

      To wait until the object loads correctly, you can use the code below. It waits up to 10 seconds for "myDynamicElement" and then throws an exception.

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
      from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

      driver = webdriver.Chrome()
      driver.get("http://somedomain/url_that_delays_loading")
      try:
      element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))
      finally:
      driver.quit()


      Delete
  3. hi i have tried to keep wait for about 100Sec Thread.sleep(100000) and then tried to find the element but then also i am getting the same error :-"InvalidElementStateException: {"errorMessage":"Element is not currently interactable and may not be manipulated"

    ReplyDelete
    Replies
    1. i am using Phantomjs with selenium webdriver language java

      Delete
    2. Waiting 100sec is to much for test automation. There should be something wrong in your test code. I think you should check the web element manually if you can reach it.

      Instead of giving time to sleep, you can use WebDriverWait method which is waiting until the web element is visible.


      public WebElement findDynamicElement(By by, int timeOut) {
      WebDriverWait wait = new WebDriverWait(driver, timeOut);
      WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
      return element;
      }

      findDynamicElement(By.xpath("//body") , 30);

      Delete
    3. hi

      I tried this code in place of thread.sleep(100000):

      WebDriverWait wait = new WebDriverWait(driver, 100);
      element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"myLogin_myUsername\"]")));
      element.clear();
      element.sendKeys("abc@123");

      But getting the same error :-
      Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 100 seconds waiting for element to be clickable: By.xpath: //*[@id="myLogin_myUsername"]

      I checked that this id is present or not in the page :
      I got only one link with "id = myLogin_myUsername"
      So no chance that this element is not present in the page


      Delete
    4. Since phantomjs provide the facility of screenshot i tried to get a screenshot for the same i see as error :- your browser is currently set to block cookies .Please enable them to login to the Website. So i am little confused that in selenium webdriver is saying "InvalidElementStateException" and in screenshot it is saying of cookies error. do any one faced this kind of issue ??

      Delete
    5. You can add the following:

      phantom.cookiesEnabled = true;

      Delete
    6. yeah its true but valid for phantomjs.exe ,
      while i am doing scripting with java + selenium here i have used :-

      String phantombinary = "D:\\Phantomjs\\phantomjs-1.9.7-windows\\phantomjs.exe" ;
      DesiredCapabilities caps = DesiredCapabilities.phantomjs();
      caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantombinary);
      caps.setCapability("phantomjs.page.cookiesEnabled", true);

      but i don't think this is the correct way of doing it .

      Delete

Post a Comment

Popular posts from this blog

Change Default Timeout and Wait Time of Capybara

Page-Object Pattern for Selenium Test Automation with Python

Performance Testing on CI: Locust is running on Jenkins

How to Set Shared Preferences in Espresso Test for Kotlin and Java