Selenium with Python: send_keys() doesn't work on headless ubuntu
Asked Answered
C

1

1

I am using python Selenium, with headless ubuntu at digitalocean, which has headless Chrome on it. I used

driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

at senium.webdriver.common.keys

But it doesn't work.

I imported everything needed, with no python syntax error, and ran successfully, but tabs are not switched with my code.

driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')

also doesn't work. The same code can switch between tabs on my local computer, which has physical keyboard and monitor. Btw, I used pyvirtualdisplay with my headless Chrome.

I suspect that using headless Ubuntu and headless Chrome on it may cause this problem. I guess headless Ubuntu can' t send keys, as the code above directed.

How can I make my remote, headless Ubuntu send keys to the browser?

Crockery answered 13/1, 2017 at 10:48 Comment(0)
A
5

This is well-known issue of chromedriver. Comment from Chromium developer's team

This is a limitation in the way we simulate keyboard input in ChromeDriver. Keys get sent directly to the render process, bypassing the browser process. So any keyboard shortcut handlers in the browser process will not be invoked by sendKeys().

You can use following code instead:

driver.execute_script("window.open('url_of_page_to_get', 'new_window')")

This will allow you to open URL in new tab

P.S. Please mark this answer as "Accepted" if it solved your problem or let me know in case of issues

Astrobiology answered 13/1, 2017 at 11:15 Comment(4)
Hi, I installed headless Firefox and its driver on headless Ubuntu, and used driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB), But this also doesn't work. I don't know why.Crockery
Traceback (most recent call last): File "tabTest.py", line 14, in <module> driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't') File "/root/ladder_selenium/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 347, in send_keys self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)}) raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotVisibleException: Message: Element is not visibleCrockery
Try to import time and wait for few seconds with time.sleep(3). Still same exception?Astrobiology
Thank you for answering. Actually, I could fix that error. I had to enter an website first and look for body element. (driver.find_element_by_tag_name('body').send_keys()) Although there is no error with Firefox, the send_keys code seems to not working, just like headless Chrome.Crockery

© 2022 - 2024 — McMap. All rights reserved.