0

I am trying to extract URLs, open them into a new tab and then perform some actions. My code is-

urls = self.driver.find_elements_by_xpath('//div[@id="maincontent"]/table/tbody/tr/td//a[@href]')
        for url in urls:

            try:
                href = url.get_attribute("href")[28:]
                print href
                actions = ActionChains(self.driver)
                url_1 = self.driver.find_element_by_xpath('//*[@href="'+href+'"]')
                actions.key_down(Keys.CONTROL).click(url_1).key_up(Keys.CONTROL).perform()
            except Exception, e:
                print e

        actions1 = ActionChains(self.driver)      
        actions1.key_down(Keys.CONTROL).key_down(Keys.TAB).key_up(Keys.TAB).key_up(Keys.CONTROL).perform()
        print "hi"
        show_report = self.driver.find_element_by_xpath('//*[@value="Show Report(s)"]')
        show_report.click()

It opens the URLs in new tabs but is not performing action on them. What should I do??

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
V.Khakhil
  • 285
  • 5
  • 22

1 Answers1

4

You'll need to change focus to the new tab as they usually get opened in the background. Then you'll need to grab a handle for the now current tab.

This is described here:

import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from time import sleep    

browser = webdriver.Firefox()
browser.get('https://www.google.com?q=python#q=python')
first_result = ui.WebDriverWait(browser, 15).until(lambda browser: browser.find_element_by_class_name('rc'))
first_link = first_result.find_element_by_tag_name('a')

# Save the window opener (current window, do not mistaken with tab... not the same)
main_window = browser.current_window_handle

# Open the link in a new tab by sending key strokes on the element
# Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack 
first_link.send_keys(Keys.CONTROL + Keys.RETURN)

# Switch tab to the new tab, which we will assume is the next one on the right
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

# Put focus on current window which will, in fact, put focus on the current visible tab
browser.switch_to_window(main_window)

# do whatever you have to do on this page, we will just got to sleep for now
sleep(2)

# Close current tab
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')

# Put focus on current window which will be the window opener
browser.switch_to_window(main_window)
Ouroborus
  • 16,237
  • 4
  • 39
  • 62
  • Keys like send_keys(Keys.CONTROL + 'w') , send_keys(Keys.CONTROL + 't') are not working – V.Khakhil Nov 07 '16 at 06:34
  • @V.Khakhil You'll need to use whatever keys are appropriate for your browser and OS. `ctrl+w` and `ctrl+t` should work across the three main browsers in Windows. – Ouroborus Nov 07 '16 at 06:53
  • i am using window 7 and chrome web driver but still these keys not working – V.Khakhil Nov 07 '16 at 06:57
  • [This answer](http://stackoverflow.com/a/28716311/367865) lists some other ways to switch tabs. – Ouroborus Nov 07 '16 at 07:00