1

I have been trying to switch tabs on chrome by using the following code.

driverlist = driver.window_handles
driver.switch_to_window(driverlist[1])

which works perfectly fine. But I want write a function when called switches to the next immediate tab, which is I'm trying to use send_keys functions like

 from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB) 

I'm expecting send_keys() to switch the tab focus to the next tab but when i print current_url, the focus stays on the previous tab.

Girish venkata
  • 704
  • 1
  • 7
  • 15
  • I think it should be .send_keys(Keys.CONTROL, Keys.TAB) – Roomm Aug 28 '18 at 14:58
  • I have tried that one too, but it simply doesn't do anything – Girish venkata Aug 28 '18 at 15:00
  • `driver.switch_to_window()` should switch the windows while `send_keys(Keys.CONTROL + "t")` open new window. These are not the same... What exactly *doesn't work*? – Andersson Aug 28 '18 at 15:02
  • As i mentioned in the title send_keys(Keys.CONTROL) doesn't work...send_keys(Keys.CONTROL + "t") is just another example that i have tried. – Girish venkata Aug 28 '18 at 15:04
  • @Girishvenkata , what do you mean by *"doesn't work."*? What you expect `send_keys()` to do in this case? – Andersson Aug 28 '18 at 15:07
  • `send_keys(Keys.CONTROL)` doesn't work isn't very specific or clear. How would you validate if just the CTRL key was pressed? Please edit your question, including the title, and clarify – JeffC Aug 28 '18 at 15:08
  • @Andersson i want send_keys() to shift the focus of the driver – Girish venkata Aug 28 '18 at 15:13
  • @JeffC my apologies i have edited the title now. I just want send_keys() function to shift the tab focus to the next tab – Girish venkata Aug 28 '18 at 15:14
  • could you please edit the question and add your error? – Carlo 1585 Aug 28 '18 at 15:15
  • If `.switch_to_window()` is working, why are you trying to use CTRL+T? – JeffC Aug 28 '18 at 15:17
  • @Girishvenkata , you want WebDriver to physically switch to another tab? – Andersson Aug 28 '18 at 15:20
  • @JeffC i want to write a function for it and I can't go by specifying the window index everytime i use it. Instead CTRL + TAB (not CTRL+T) would automatically switch it right? – Girish venkata Aug 28 '18 at 15:20
  • @Andersson Yes that's what i'm trying to do – Girish venkata Aug 28 '18 at 15:21
  • Why can't you specify the index each time? Just write a function that stores the current window handle and loops through window handles until you have a handle that isn't current. Now you know that you have switched handles. – JeffC Aug 28 '18 at 15:41
  • @JeffC The function i'm writing is a part of script that is used for automated testing, that is the reason i want to skip sepcifying the index. How can I know which handle is the current one? – Girish venkata Aug 28 '18 at 15:46
  • `driver.current_window_handle` from [the docs](https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.current_window_handle) – JeffC Aug 28 '18 at 16:37

0 Answers0