如何使用 Selenium 打开新选项卡?

c0d*_*x27 -1 python macos firefox selenium selenium-webdriver

使用 MAC OS 我尝试使用 selenium firefox webdriver 打开新选项卡

url = "https://google.com" 

mail = wd.Firefox()  

mail.get(url)

 time.sleep(1)

 actions = ActionChains(mail) mail.find_element_by_xpath("/html/body/div[2]/div[2]/div[1]/h2/span[2]/span[1]").click() 

time.sleep(1) 

ActionChains(mail).key_down(Keys.COMMAND).send_keys('t').key_up(Keys.COMMAND).perform()  

mail.close()

 mail.quit()
Run Code Online (Sandbox Code Playgroud)

sup*_*uri 5

如果您使用 chrome/firefox,您可以运行此 javascript。

window.open("https://google.com","_blank")
Run Code Online (Sandbox Code Playgroud)

Python:

driver.execute_script("window.open('https://google.com','_blank')")
Run Code Online (Sandbox Code Playgroud)