简体   繁体   中英

New Tab using Selenium WebDriver

How do I open a new tab using Selenium WebDriver all the other answers I found were out of date and/or inaccurate.

I've tried:

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

but this does not work reliably and needs manual switching to the new tab that's unreliable

You could use your send_keys method to open a new tab, and I believe Selenium also offers a function to switch tabs.

Yeah so Finally I found a foolproof solution to this problem the solution is actually there in the official Selenium website but this worked perfectly for me

  • Update selenium to its alpha version using pip install selenium==4.0.0.a7

Link to downloads page on official selenium website

Yeah and then you can create a new tab using this command:

Link to Selenium Documentation on it's offical website

# Opens a new tab and switches to new tab
driver.switch_to.new_window('tab')

# Opens a new window and switches to new window
driver.switch_to.new_window('window')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM