0

The problem :

Simply start a .py with: driver = webdriver.Chrome() Open another tab manualy and use :

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

It won't switch tabs.

If you open the tab using :

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

The problem isn't there, but won't fix my issue. I need to open tabs manualy and switch later using selenium.

Using the command tab send_key works fine when using firefox.

Any suggestions?

Example 1:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()

# Open 2 extra tabs
driver.get("http://www.google.com")
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + "t")
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + "t")

# Switch tabs
time.sleep(2)
print "Press TAB"
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
time.sleep(2)
print "Press TAB"
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
time.sleep(2)
print "Press TAB"
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

Example 2:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("http://www.google.com")

raw_input("Open 2 tabs manually")

# Switch_to_window won't help
#firstpage = driver.window_handles[0]
#driver.switch_to_window(firstpage)

time.sleep(2)
print "Press TAB"
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
time.sleep(2)
print "Press TAB"
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
time.sleep(2)
print "Press TAB"
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

Example 1 Works. In example 2 the send_keys is ignored.

Update:

If you switch to the handle you want to focus use:

driver.execute_script('alert("Focus window")')
driver.switch_to.alert.accept()
  • Consider formatting your question, for instance putting the code between quotes ``. It will be easier to read and understand, therefore you'll have more answers. – ysearka Jun 30 '16 at 12:26
  • `driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)` this line will assume and switch the new tab is the next one on the right.... – Saurabh Gaur Jun 30 '16 at 12:30
  • Yes, but when opening a tab manually (the second one) send_keys isn't working anymore in my case. (using chromium) – Dion Koens Jun 30 '16 at 12:32
  • try this and let me know ..`actions = ActionChains(driver) actions.key_down(Keys.CONTROL).key_down(Keys.TAB).key_up(Keys.TAB).key_up(Keys.CONTROL).perform()` – Saurabh Gaur Jun 30 '16 at 12:33
  • Won't work. It's not responding after opening a new tab manually. – Dion Koens Jun 30 '16 at 12:42
  • @DionKoens Why do you want to open the tab? You can switch the `window_handle` of the `driver` and perform your actions on the page just fine. – RemcoW Jun 30 '16 at 12:50
  • Yes, i know. That works great. But I can't get the active tab in "front" that way. I want to show the user the page the program is working on. – Dion Koens Jun 30 '16 at 13:05
  • 2
    Possible duplicate of [Handle multiple window in Python](http://stackoverflow.com/questions/10629815/handle-multiple-window-in-python) – JeffC Jun 30 '16 at 14:11
  • @JeffC No, in that example the tab is openend by selenium. That will work. I'am opening the tab manually. It's a chrome problem (it think), it works fine with firefox. – Dion Koens Jun 30 '16 at 14:24
  • I was referring more to using the window handles code instead of keystrokes. – JeffC Jun 30 '16 at 14:51
  • @JeffC. I added 2 examples. Maby my question wasn't correct. – Dion Koens Jun 30 '16 at 15:43
  • I understand your question. What I'm trying to say is that you are using send_keys() to send key strokes to move between tabs. If you look at the link that I posted, there is code there to use window handles to switch between tabs. I would suggest that you try that code and see if it works. It should be more reliable than send_keys(). I would say that, in general, send_keys() is probably the least reliable way to do just about anything. – JeffC Jun 30 '16 at 15:50
  • @JeffC: It's no problem to switch tabs using window handles, works fine. But I want to bring the tab in front which needs attention. Now I have to print a message "Tab 3 needs attention". It would be nice to jump to the right tab using Keys.CONTROL + TABNR. In background he is allready there using window handles, so i now the right tab nr. But if the send_keys thing is buggy, i'll skip that beautifull plan. ;) – Dion Koens Jun 30 '16 at 15:57
  • Found a working sollution. – Dion Koens Jun 30 '16 at 18:15

0 Answers0