1

I am trying to open URL found based on link_text in a loop. The below is the program that I am trying to use. Actually, what happening, actually on that page I have details 3 times, some times it will be 4 times (it's dynamic).

Updated Code:

from selenium.webdriver.support import ui
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options=options, executable_path='C:\chromedriver_win32\chromedriver.exe')
driver.maximize_window()
driver.get("https://xxxxxx/blogs/")
if driver.find_element_by_xpath("(//span[@class='ui-datepicker-month'][contains(.,'May')])[1]"):        
    # get the number of details to click
    addr = driver.find_elements_by_link_text('Details')
    urls = [v.get_attribute("href") for v in addr]
    for x in range(1, len(urls) + 1):
        driver.execute_script("window.open();")
        driver.switch_to.window(driver.window_handles[x])
        driver.get(urls[x - 1])`

Output is:

Its working correctly, open all detail href in new tabs.

Update 2: As per Dmitri T code, now its working opening all details href in new tabs :) thanks for help. One final I would like to try is in datepicker loop, i have hardcoded the date as //span[@class='ui-datepicker-month'][contains(.,'May')])[1]" in may month. How can i loop through this i.e to click on each day means. click on 1st may,open all "details" href in each newtab,go to main url again, click on 2nd May, do the same stuff...open all "details" href in new tabs...so on..I am trying to write code...let you know results. thanks you experts.

3
  • Are you getting an error message? Also, are you using python 2 or 3? Commented May 9, 2019 at 17:00
  • Hi thanks for reply, I am using 3.7 version of python . No error., it opening main first then nothing happen after that...however, when i checked/inspected, I found xpath for that 'Details' is (//a[@class='details'][contains(.,'Details')])[1],(//a[@class='details'][contains(.,'Details')])[2], (//a[@class='details'][contains(.,'Details')])[3].. so if there are 3 details instances, then it have values like above..so I need to iterate over all instances of that detail then open those in each in new tabs Commented May 9, 2019 at 17:07
  • currently its opening main url, then opening a new blank tab. Commented May 9, 2019 at 17:14

2 Answers 2

2
  1. Use For loop to iterate the URLs from the addr list
  2. Use get_attribute function to extract the URL from the web element

Assuming above hints you need to amend the code like:

addr = driver.find_elements_by_link_text('Details')
urls = [v.get_attribute("href") for v in addr]
for x in range(1, len(urls) + 1):
    driver.execute_script("window.open();")
    driver.switch_to.window(driver.window_handles[x])
    driver.get(urls[x - 1])

You might also want to consider re-implementing your test to use Page Object pattern - this way it will be way easier to maintain given you split test logic from UI part.

Sign up to request clarification or add additional context in comments.

4 Comments

I am giving it a try now
I tried this logic,its open main url --> then open a first Details link new tab --> then fail with selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document. I guess am bit closer to goal..
This is not a code-writing service, this time I updated my answer to resolve this issue with stale element
My apologies @Dmitri. I tried so many things reading post on this topic on stack overflow, after so many failures in my coding i posted question here. I am newbie in selenium testing and learning day by day. As there so many experts here,i do learn something. anyhow thanks you very much for help
1

You should be able to open all the details links in new tabs using the below logic.

driver.get("https://xxxxxx/blogs/")
if driver.find_element_by_xpath("(//span[@class='ui-datepicker-month'][contains(.,'May')])[1]"):
    main_window = driver.current_window_handle
    # get the number of details to click
    addr = len(driver.find_elements_by_xpath("//a[@class='details'][contains(.,'Details')]"))
    # iterate through all the details links  (used the index rather elements list as it may lead to staleeleemnt exception after clicking on the first detiails link)
    for addrNum in range(addr):
        # get the details element based on index
        ele = driver.find_element_by_xpath("(//a[@class='details'][contains(.,'Details')])[" + str (addrNum+1) + "]")
        # get the href of the link
        href = ele.get_attribute('href')
        # open the href in another tab
        driver.execute_script("window.open('" + href +"');")
        # switching to parent window (on safer side)
        driver.switch_to.window(main_window)

6 Comments

I am getting below error addr = len(driver.finds_elements_by_xpath("//a[@class='details'][contains(.,'Details')]")) AttributeError: 'WebDriver' object has no attribute 'finds_elements_by_xpath'
there is a typo finds_elements_by_xpath is not a method it should be find_elements_by_xpath. Updated the answer, please check with the updated answer.
This time I get href = ele.get_attribute('href') AttributeError: 'list' object has no attribute 'get_attribute'
hello sir, I got selenium.common.exceptions.JavascriptException: Message: javascript error: aruguments is not defined ...this error message this time. sorry to bother you i am newbee..still learning..
No worries, can you please check with the updated code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.