Reputation: 931
Are there is any function abolished by Selenium ?
Goal : open new tab like "Ctrl + t"
Environment:
No Reaction of both script below :
# 1
dr.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
# 2
ActionChains(dr).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
Some tutorial video 3~4yrs ago showing the scripts below worked but not in my case
Upvotes: 2
Views: 2095
Reputation: 11
I tried all 3 methods.
Nothing happens: no new tab.
I trust the window.open method, because it works if I use _self instead of _blank. Somehow, Selenium seems to block any method that opens a new tab.
Upvotes: 1
Reputation: 89
The question is the new gecko driver (marionette), has two contexts:
1) Chrome (The browser itself)
2) Content (The webpage content)
You need to tell selenium in which context do you want to send the action.
For me (Python 3.6, Selenium 3.141, gecko driver 0.26.0, firefox 75) this code works:
driver.execute("SET_CONTEXT", {"context": "chrome"})
urlbar=driver.find_element('id','urlbar')
urlbar.send_keys(Keys.CONTROL, "t")
driver.execute("SET_CONTEXT", {"context": "content"})
Upvotes: 3
Reputation: 7563
You can open a new tab in the following ways :
SendKey method
Mac OS
dr.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
Other OS
dr.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
Action method
ActionChains(dr).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
ExecuteScript method
dr.execute_script("window.open('','_blank');")
Or with specific url
dr.execute_script("window.open('URL');")
--UPDATE--
If your issue is to want to switch to a particular tab, then do this :
Before do your actions, handles your current tab with :
first_tab = dr.window_handles[0]
Then do your actions which will bring to a new tab. May .click
action.
Add some sleep for wait load new tab dr.implicitly_wait(....)
.
Handle new tab window with :
second_tab = dr.window_handles[1]
Now you can swith to you want tab :
dr.switch_to.window(first_tab / second_tab)
Hope this helps.
Upvotes: 2