1

I am new to Selenium.. I am trying to get the href link which is present inside two div elements

<div class="abc def">
 <div class="foo xyz">
 <a href="/watch123" ></a>
 </div>
</div>

How do I get the href link? I have tried

persons = []
for person in item.find_elements_by_class_name('abc.def'):
 title = person.find_element_by_xpath('.//div[@class="foo.xyz"]/a').text
 link = title.get_attribute("href")

but it does not work

JeffC
26.5k5 gold badges35 silver badges57 bronze badges
asked Jul 5, 2016 at 20:49

1 Answer 1

4

Try this: assuming your browser driver is called "driver" and all your elements' attributes are unique.

  • Xpath:

    element = driver.find_element_by_xpath("//div[@class='foo xyz']/a")
    link = element.get_attribute("href")
    
  • Css selector:

    element = driver.find_element_by_css_selector("div[class='foo xyz']>a")
    link = element.get_attribute("href")
    
JeffC
26.5k5 gold badges35 silver badges57 bronze badges
answered Jul 5, 2016 at 21:38
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.