Open Url in new tab and tab, switching in IE with python and selenium

I am trying to open a new tab in IE10 and selenium 2.45. It can open a new tab using pyrobot. But when I try to open the url in a new tab, it opens in the first tab. The focus is not set to the second tab and hence does not work, and also tab switching does not work. please provide a 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 the links don't open in a new tab, and the switch doesn't happen either. All links open on the first tab.

+3


source to share


2 answers


Seems to be switch_to_window

deprecated in 2.45. Use instead switch_to.window

.

Code taken from 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)

      

+3


source


Try this code below:



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

      

-1


source







All Articles