user3252344
user3252344

Reputation: 758

Python 3 + Selenium 3: ActionChain use in a loop throws WebDriverException

I am trying to make a program which will go to a page that has a lot of links, open the first X many links in new tabs and do something on those pages.

But using ActionChains to open the pages is causing this exception on the second iteration of the loop:

Traceback (most recent call last):
  File "autoPage.py", line 74, in <module>
    ActionChains(driver).key_down(Keys_COMMAND).click(link).key_up(Keys_COMMAND).perform()
  File "usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/action_chains.py", line 80, in perform
    self.w3c_actions.perform()
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/actions/action_builder.py", line 76, in perform
    self.driver.execute(Command.W3C_ACTIONS, enc)
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: TypeError: inputState is undefined

I've tried this solution and this solution to a similar issues with no luck, and I'm basically doing this code snippet on github but in a loop.

Here is the script:

driver = webdriver.Firefox()
driver.get("the website")
main_window = driver.current_window_handle

numLinksToGoThrough = 2;
text = "keyword"
for i in range(numLinksToGoThrough):
    print("I=i")
    link = driver.find_elements_by_xpath('//a[contains(@data-automation, "%s")]' % text)[i]
    ActionChains(driver).key_down(Keys.COMMAND).click(link).key_up(Keys.COMMAND).perform()

    sleep(3); #wait for page to load a bit

    driver.switch_to_window(main_window)

    print("Handle job action")

    sleep(2); # placeholder

    print("End handle job action")
    driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')
    browser.switch_to_window(main_window)

Upvotes: 1

Views: 452

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193078

This error message...

ActionChains(driver).key_down(Keys_COMMAND).click(link).key_up(Keys_COMMAND).perform()
.
selenium.common.exceptions.WebDriverException: Message: TypeError: inputState is undefined

...implies that the GeckoDriver was unable to perform() the Actions within the Browsing Context i.e. Firefox Browser session.


However, to open the new tabs through Ctrl and click() simultaneously instead of using Keys.COMMAND you need to use Keys.CONTROL as follows:

ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()

References

You can find a couple of relevant discussions in:

Upvotes: 1

Related Questions