python - 使用 python 和 selenium 在 IE 中的新选项卡和选项卡切换中打开一个 Url

标签 python selenium internet-explorer-10

我正在尝试在 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/

相关文章:

websocket - IE10 握手后关闭连接

c# - 如何在 Windows8 笔记本电脑上禁用捏合缩放手势

python - 如何合并数值和嵌入序列模型以处理 RNN 中的类别

flash - 在 saucelabs/selenium webdriver 中禁用 flash?

windows-7 - Windows 7 上的 IE10 并排 IE8

python - 无法从网页获取某些表格数据的内容

python - 尝试在 Python 中使用 Selenium 启动网页

python - 如何在 Python 中获取已排序数组的索引

python - 优雅地停止 celery 任务

python - 火狐市场 API : app icon stays default after successful upload