使用python和selenium在IE中打开一个Url,在IE中切换

时间:2015-05-05 08:08:16

标签: python selenium internet-explorer-10

我正在尝试在IE10和selenium 2.45中打开一个新标签。它可以使用pyrobot打开一个新选项卡。但是当我尝试在新标签中打开网址时,它会在第一个标签中打开。焦点未设置为第二个选项卡,因此它不起作用,并且选项卡的切换也不起作用。请提供解决方案。我在下面提供了我的代码:     代码:

# 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)

但链接没有在新标签中打开,切换也没有发生。所有链接都在第一个标签中打开。

2 个答案:

答案 0 :(得分:3)

在2.45版本中看起来不推荐使用switch_to_window。请改用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)

答案 1 :(得分:-1)

请尝试以下代码:

import selenium.webdriver as webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")

stackele = driver.find_element_by_id("blurb")
if stackele.is_displayed():
    stackele.send_keys(Keys.CONTROL + 't')
    driver.get("https://www.python.org/")

pythele = driver.find_element_by_link_text("Python")
if pythele.is_displayed():
    pythele.send_keys(Keys.CONTROL + Keys.TAB)
    driver.get("https://www.yahoo.com/")

yahooele = driver.find_element_by_link_text("Yahoo")
if yahooele.is_displayed():
    yahooele.send_keys(Keys.CONTROL + Keys.TAB)
    driver.get("https://www.gmail.com/")