Python-使用Selenium打开多个选项卡 [英] Python -- Opening multiple tabs using Selenium

查看:543
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆