简体   繁体   中英

Error when opening new tab and grabbing content from that tab using XPath with Selenium

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.

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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