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?

heyzude
  • 363
  • 1
  • 4
  • 20

1 Answers1

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

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • 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. – heyzude Jan 14 '17 at 16:01
  • Traceback (most recent call last): File "tabTest.py", line 14, in 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 visible – heyzude Jan 14 '17 at 16:03
  • Try to `import time` and wait for few seconds with `time.sleep(3)`. Still same exception? – Andersson Jan 14 '17 at 16:34
  • 1
    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. – heyzude Jan 15 '17 at 16:28