我正在尝试在 IE10 和 selenium 2.45 中打开一个新选项卡。它能够使用 pyrobot 打开一个新选项卡。但是当我尝试在新选项卡中打开 url 时,它会在第一个选项卡中打开。焦点未设置到第二个选项卡,因此它不起作用,并且选项卡切换也不起作用。请提供解决方案。我在下面提供了我的代码: 代码:
# Open first tab
IEDriverPath = "/../../../../../../../IEDriverServer.exe"
driver = webdriver.Ie(IEDriverPath, port=5555)
pyseldriver.get("https://www.google.com/")
time.sleep(5)
tab1 = pyseldriver.current_window_handle
#open another tab
obja = pyrobot.Robot()
obja.key_press(pyrobot.Keys.ctrl)
obja.key_press(pyrobot.Keys.t)
obja.key_release(pyrobot.Keys.ctrl)
obja.key_release(pyrobot.Keys.t)
time.sleep(FrameworkConstants.Time02)
pyseldriver.switch_to_window(pyseldriver.window_handles[-1])
tab2 = pyseldriver.current_window_handle
pyseldriver.get("https://www.python.org/")
time.sleep(5)
#Switching to first tab and opening new url
pyseldriver.switch_to_window(tab1)
pyseldriver.get("https://www.yahoo.com/")
time.sleep(10)
#switching to second tab and opening new url
pyseldriver.switch_to_window(tab2)
pyseldriver.get("https://www.gmail.com/")
time.sleep(10)
但是链接没有在新标签页中打开,切换也没有发生。所有链接都在第一个选项卡中打开。
最佳答案
看起来 switch_to_window
在 2.45 版中已弃用。请改用 switch_to.window
。
代码取自 webdriver.py
。参见 this
def switch_to_active_element(self):
""" Deprecated use driver.switch_to.active_element
"""
warnings.warn("use driver.switch_to.active_element instead", DeprecationWarning)
return self._switch_to.active_element
def switch_to_window(self, window_name):
""" Deprecated use driver.switch_to.window
"""
warnings.warn("use driver.switch_to.window instead", DeprecationWarning)
self._switch_to.window(window_name)
关于python - 使用 python 和 selenium 在 IE 中的新选项卡和选项卡切换中打开一个 Url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30047443/