簡體   English   中英

使用python和selenium在IE中打開一個Url,在IE中切換

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

我試圖在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.45中不推薦使用switch_to_window 請改用switch_to.window

代碼取自webdriver.py 看到這個

 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)

試試以下代碼:

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