Python -- 使用 Selenium 打开多个选项卡 [英] Python -- Opening multiple tabs using Selenium
本文介绍了Python -- 使用 Selenium 打开多个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Python.我正在尝试在 chrome 上打开两个选项卡,每个选项卡都指向不同的网站.这是我的代码:
I am using Python. I am trying to open two tabs on chrome, each to a different website. This is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser=webdriver.Chrome()
browser.get('http:/reddit.com')
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
time.sleep(3)
browser.get('http://bing.com')
当我运行它时,第一个选项卡打开到 reddit.com,然后另一个选项卡打开到我的默认网页,然后 bing.com 在原始选项卡中打开.我希望第一个选项卡转到 Reddit,第二个选项卡转到 bing,但 browser.get('website')
只对第一个选项卡起作用.
When I run it, the first tab is opened to reddit.com, and then another tab opens to my default webpage, and then bing.com is opened in the original tab. I want the first tab to go to Reddit and the second tab to go to bing, but browser.get('website')
only acts on the first tab.
推荐答案
在 python 上试试这样:
try like this for python:
browser=webdriver.Chrome()
browser.get('http:/reddit.com')
window_before = driver.window_handles[0]
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
time.sleep(3)
browser.get('http://bing.com')
这篇关于Python -- 使用 Selenium 打开多个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文