0

What I want to do:

I want to click all the links where partial text = "105611A5C7B9_Logs_"

Example:

First Link Text = 105611A5C7B9_Logs_11-15-18-06-04PM.tgz
Second Link Text = 105611A5C7B9_Logs_11-15-18-11-09PM.tgz
Third Link Text = 105611A5C7B9_Logs_11-15-18-09-18AM.tgz

If I click these links, it will download the files. I want to click all the file links which matches the partial text = "105611A5C7B9_Logs_"

HTML:

<a href="javascript:download('2018-11-15','105611A5C7B9_Logs_11-15-18-06-04PM.tgz','','105611A5C7B9','/2018/11/15/18/')">105611A5C7B9_Logs_11-15-18-06-04PM.tgz</a>

<a href="javascript:download('2018-11-15','105611A5C7B9_Logs_11-15-18-11-09PM.tgz','','105611A5C7B9','/2018/11/15/23/')">105611A5C7B9_Logs_11-15-18-11-09PM.tgz</a>

<a href="javascript:download('2018-11-15','105611A5C7B9_Logs_11-15-18-09-18AM.tgz','','105611A5C7B9','/2018/11/15/9/')">105611A5C7B9_Logs_11-15-18-09-18AM.tgz</a>  

What I tried:

link="105611A5C7B9_Logs_"

try:
    print(driver.find_element_by_partial_link_text(link).text)
    driver.find_element_by_partial_link_text(link).click()
except:
    print("Not Found")

This clicks the first link only. How can I click all the links that matches the partial text ?

2 Answers 2

2

It is mentioned in selenium specification that find_element will locate the very first element found on your webpage. To grab all elements you have to use find_elements

In your code you are using find_element_by_partial_link_text instead of find_elements_by_partial_link_text that's why getting only first element.

You should try with find_elements_by_partial_link_text it will return a list of WebElement.

You have to loop through that list to iterate all element which you are expecting.

2
  • Thanks for the detailed explanation. I solved it. Commented Nov 16, 2018 at 12:23
  • 1
    @DipankarNalui please mark NarendraR's answer as the accepted answer since it has solved your problem. This will help others who have the same problem.
    – Kate Paulk
    Commented Nov 16, 2018 at 12:25
0

From the above answer of @NarendraR, I understand that find_element will give the first match only. Here I need to use find_elements to get all matched elements.

Here is the solution:

link="105611A5C7B9_Logs_"
try:
    elements=driver.find_elements_by_partial_link_text(link)
    for element in elements:
        print(element.text)
        element.click()
except:
    print("Not Found")

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.