1
votes

Getting error: "invalid element state" when using Chrome driver for selenium.

What I'm trying to do: Pass in some data to http://www.dhl.de/onlinefrankierung

My first issue is that when I try to use the .click() method on the checkbox named "Nachnahme" nothing happens, no check is made.

When you make a check manually, the page refreshes and additional fields open up, which is what I'm trying to access.

The second issue, which throws the invalid element state, happens when trying to pass in data using the .send_keys() method.

Here is my code so far:

from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\Owner\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages\\chromedriver.exe')
driver.get('http://www.dhl.de/onlinefrankierung')
product_element = driver.find_element(by='id', value='bpc_PAK02')
product_element.click()
services_element = driver.find_element(by='id', value='sc_NNAHME')
services_element.click()
address_element_name = driver.find_element(by='name', value='formModel.sender.name')
address_element_name.send_keys("JackBlack")

ERROR: C:\Users\Owner\AppData\Local\Programs\Python\Python36-32\python.exe "C:/Users/Owner/Desktop/UpWork/Marvin Sessner/script.py" Traceback (most recent call last): File "C:/Users/Owner/Desktop/UpWork/Marvin Sessner/script.py", line 23, in address_element_name.send_keys("tester") File "C:\Users\Owner\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webelement.py", line 352, in send_keys 'value': keys_to_typing(value)}) File "C:\Users\Owner\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webelement.py", line 501, in _execute return self._parent.execute(command, params) File "C:\Users\Owner\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute self.error_handler.check_response(response) File "C:\Users\Owner\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidElementStateException: Message: invalid element state (Session info: chrome=HIDDEN) (Driver info: chromedriver=HIDDEN (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.15063 x86_64)

2
Can you update the question with the complete error stack trace and for which element and at which line? - DebanjanB
I just updated the question - Robert Bailey

2 Answers

2
votes

Putting just a small sleep between two actions solve the issue. Following code working perfectly fine.

Now before someone downvotes or put a comment about sleep. let me clarify, is this the best solution? No, it's not

But now you know why it was not working, Your action is generating some AJAX request and before it completes you are trying to do another action which is creating the issue.

The good solution would be to write the condition, which waits until that action is complete but meanwhile you have a working temporary solution.

import time
from selenium import webdriver
driver = webdriver.Chrome('h:\\bin\\chromedriver.exe')
driver.get('http://www.dhl.de/onlinefrankierung')
product_element = driver.find_element(by='id', value='bpc_PAK02')
product_element.click()
time.sleep(5)
services_element = driver.find_element(by='id', value='sc_NNAHME')
services_element.click()
time.sleep(5)
address_element_name = driver.find_element(by='name', value='formModel.sender.name')
address_element_name.send_keys("JackBlack")
1
votes

If you use explicit waits, you can often avoid this error. In particular, if an element can be clicked (an input, button, select, etc.), you can wait for it to be clickable. Here is what will work in your case.

from selenium import webdriver
from selenium.webdriver.common import utils
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def waitForElementClickable(timeout=5, method='id', locator=None):
    try:
        element = WebDriverWait(driver, timeout).until(
            EC.element_to_be_clickable((method, locator))
        )
        print("Element", method + '=' + locator, "can be clicked.")
        return element
    except Exception:
        print("Element", method + '=' + locator, "CANNOT be clicked.")
        raise

options = Options()
options.add_argument('--disable-infobars')
driver = webdriver.Chrome(chrome_options=options)
driver.get('http://www.dhl.de/onlinefrankierung')

product_element = waitForElementClickable(method='id', locator='bpc_PAK02')
product_element.click()

services_element = waitForElementClickable(method='id', locator='sc_NNAHME')
services_element.click()

address_element_name = waitForElementClickable(method='name', locator='formModel.sender.name')
address_element_name.send_keys("JackBlack")