3

I am using Selenium in python. I have lists of hyperlinks from where I am iterating over each hyperlink and opening that particular link in a new tab and then put focus on new tab using Keys.CONTROL + Keys.SHIFT + Keys.RETURN.

Unfortunately, opening a link in new Tab fails while using xpath selection on the new tab:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep  

list_links = self.driver(By.XPATH, "//a/" )
for link in list_links:
    link.send_keys(Keys.CONTROL + Keys.SHIFT + Keys.RETURN) #Open new Tab

    #get text from new tab webpage.
    sleep(50)
    data = self.driver.find_element(By.XPATH, "//span/" ).text #ERROR
    print data

I am able to open new tab and set focus the new tab (this appears to work). But when I try to use XPath on new tab then it throws:

NoSuchElementException: Message: no such element.

Even though the element is available there. I don't know what I am doing wrong here. Any help will be appreciable.

Abel
  • 56,041
  • 24
  • 146
  • 247
iNikkz
  • 3,729
  • 5
  • 29
  • 59
  • it looks like, still, you stay in same page. where do you change page? – Mahsum Akbas Sep 08 '15 at 16:02
  • @MahsumAkbas, I don't think so. The key-combination sent opens a new tab _with the link under the current focus_. Though I agree, it would be good to test if that works correctly in Selenium. Also, do not use `sleep(50)`. Not sure it will block the thread, but it may, and then you will never select anything. Try the [`WebDriverWait` functions](http://selenium-python.readthedocs.org/en/latest/waits.html) instead. – Abel Sep 08 '15 at 23:03
  • 1
    @Abel: I also tried `WebDriverWait` functions but still same issue. The problem, I think is that link opens in new tab and it shows current tab as focussed but I think `selenium points current active focus to previous tab` `not to the current tab`. Any help please. – iNikkz Sep 09 '15 at 05:06
  • For Windows, You can change tabs by Ctrl+Number (number of tabs; 1,2,3 etc). you can try that. – Mahsum Akbas Sep 09 '15 at 05:56
  • @MahsumAkbas: I am using Linux. I got the solution . I am posting. Thanks for your help. – iNikkz Sep 09 '15 at 07:22
  • I removed your inline bold, as it was very hard to read. Now it is quiet with less disturbances. Also better for future visitors (so much bold is typically considered "shouting" and doesn't help in understanding the problem). – Abel Sep 09 '15 at 07:50

2 Answers2

2

The following code is copied from this link and shows a few extra steps of how you can switch focus to another tab. Give it a try:

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

# alternatively, use this, but it was reported that it doesn't always work
# driver.close()

# Put focus on current window which will be the window opener
browser.switch_to_window(main_window)

If you are using a Mac, this may not work, but in the comments a workaround is given for that too.

Alternatively, this SO answer has another workaround by using the window name of the tab.


Update: as was said in the comments, the user uses Linux (not in the OP). Use the following key-combination instead of the default one mentioned in the code above: Keys.CONTROL + Keys.SHIFT + Keys.RETURN.

Community
  • 1
  • 1
Abel
  • 56,041
  • 24
  • 146
  • 247
  • **first_link.send_keys(Keys.CONTROL + Keys.RETURN) and (Keys.CONTROL + 'w') and (Keys.CONTROL + 't')**. It don't works in linux. So, I used **Keys.CONTROL + Keys.SHIFT + Keys.RETURN** to open new tab. I got the solution. I am posting.. Thanks for your help. – iNikkz Sep 09 '15 at 07:21
  • @iNikkz: a friendly tip, use `\` code here \`` (backticks) in comments, to make it easier on the eyes when you want to communicate code snippets (bold should be used _very sparingly_, italics is typically better). – Abel Sep 09 '15 at 07:52
  • Thanks. I willl take care of it. – iNikkz Sep 09 '15 at 16:22
2

Below code is to open a link in new tab and make focus on newly open tab and grab XPATH from that newly open tab.

#Set current_window_handle to main tab
main_tab = driver.current_window_handle

# Get all links
list_links = self.driver(By.XPATH, "//a/" )

#Iterate over links
for element in list_links:

  # Open link in new tab 
  link.send_keys(Keys.CONTROL + Keys.SHIFT + Keys.RETURN)

  # Switch focus to newly opened tab using driver.window_handles[1]
  # driver.window_handles[1] => [1] for new tab index.
  driver.switch_to_window(driver.window_handles[1])

  /*** Do something OR Grabbing content using XPATH ***/

  #Now close newly opened tab
  driver.close()

  #Again switch to main tab/ previous tab
  driver.switch_to_window(main_tab)
iNikkz
  • 3,729
  • 5
  • 29
  • 59
  • Yes, that is the solution I posted. You shouldn't post it twice if it is the same. Instead, if you think I made a typo (I think the send_keys, you are referring to), you can always edit my post or use a comment (as you did, tx). – Abel Sep 09 '15 at 07:42
  • @Abel: Even thought `Ctrl+"t" and Ctrl + "w" don't work`. I made it simpler. Please verify my answer. – iNikkz Sep 09 '15 at 16:23
  • Ah, I see what you mean. Well, these key combinations can be different per browser, per system, even per configuration (I have some different key combis, for instance). Would be better if Selenium had an "Open in tab" command, it'd save us a lot of trouble ;). – Abel Sep 09 '15 at 16:42