0

This is the tag containing href. This is the HTML of one of the links when I inspected it.

The code I used to for looping through the links is:

elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    print(elem)
    elem.get_attribute("href").click()

but I am getting the error:

File "C:/Users/user/Desktop/sel.py", line 31, in

(session="7896348772e450d1658543632013ca4e", element="0.06572622905717385-1")>

elem.get_attribute("href").click()

AttributeError: 'str' object has no attribute 'click'

Can any one help please.

7
  • could you share relevant html ? Commented Jun 27, 2018 at 6:51
  • @prany do you want the whole source code? Commented Jun 27, 2018 at 7:03
  • @vijayMV - not whole source code but the one relevant to your question Commented Jun 27, 2018 at 7:04
  • @prany i have attached the image of one of the links html in the post. Commented Jun 27, 2018 at 7:12
  • 1
    I can't see any href in your html Commented Jun 27, 2018 at 7:17

3 Answers 3

3

This error message...

AttributeError: 'str' object has no attribute 'click'

...implies that you script/program have attempted to invoke click() on a string object.

What went wrong

As per the line of code:

elem.get_attribute("href").click()

You have extracted the href attribute of the first element from the List elems. get_attribute() method returns a string. String data types can't invoke click() method. Hence you see the error.

Solution

Now, in all possibilities as you are extracting the href attributes and you want to open the Links and a viable solution will be to open the (href) links in the adjacent TABs as follows:

elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    print(elem)
    my_href = elem.get_attribute("href")
    driver.execute_script("window.open('" + my_href +"');")
    # perform your tasks in the new window and switch back to the parent windown for the remaining hrefs
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the new window trick. OP might want to checkout the window_handles and, switch_to from the docs
1

The problem is the get_attribute() method returns the value of the attribute. In this case, the attribute is hrefso, it returned str obj. Note that, the web element elem is clickable. But, if you click on the elem. It will take you to the next page hence, iterating over all these web elements (elems) won't be possible as, driver will move on to next page!

Alternate way, to achieve what you are looking for is to create a list of links and, iterate over it like below:

links = []   
elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    print(elem)
    links.append(elem.get_attribute("href"))

for link in links:
    driver.get(link)
    # do you stuff

This way, we are making sure to collect all the links from the web element list i.e. elems by iterating over it. After collecting all the links and storing them in the list, we iterate over the collected list of urls.

4 Comments

Good job pointing out the problem with the loop. If the user has to perform different actions on each link, he would have to add few more conditions.
@Shivam Mishra if i want to extract some information from each link what should i add more to the existing code?
Thanks Shivam Mishra. @vijayMV check DebanjanB's Answer below
@vijayMV What exactly do you want to test in each link? You could use any method in the answers. What I wanted to say was, if you use the method in this answer, you would have to check which page the driver is currently into i.e. if link==link1, perform test1 and so on. That's because I assume you have to perform different operations on different links.
0

The get_attribute("href") returns the STRING of the url whichever the element is pointing to. If you want to click on the hyperlink element, just do:

for elem in elems:
    print(elem)
    elem.click()
    driver.back() //to go back the previous page and continue over the links

On a sidenote, if you want to print the URL of the hyperlink which you are clicking, you can use your get_attribute() method:

print(elem.get_attribute("href"))

Comments

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.