简体   繁体   English

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

[英]Open a Url in new tab and tab switching in IE using python and selenium

I am trying to open a new tab in IE10 and selenium 2.45. 我试图在IE10和selenium 2.45中打开一个新标签。 It is able to open a new tab using pyrobot. 它可以使用pyrobot打开一个新选项卡。 But when i am trying to open url in new tab, it is getting opened in first tab. 但是当我尝试在新标签中打开网址时,它会在第一个标签中打开。 Focus is not set to second tab and hence it is not working and also switching of tab is not working. 焦点未设置为第二个选项卡,因此它不起作用,并且选项卡的切换也不起作用。 please provide solution. 请提供解决方案。 I have provided my code below: Code: 我在下面提供了我的代码:代码:

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

But links are not opening in new tab and switching is also not happening. 但链接没有在新标签中打开,切换也没有发生。 All links are getting opened in first tab itself. 所有链接都在第一个标签中打开。

Looks like switch_to_window is deprecated in version 2.45. 看起来版本2.45中不推荐使用switch_to_window Use switch_to.window instead. 请改用switch_to.window

Code taken from webdriver.py . 代码取自webdriver.py See 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)

Try this below code: 试试以下代码:

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/")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM