kwsong0314
kwsong0314

Reputation: 251

How to get the value of href using text

I have the following text: 'abcdef' , I want to use this text to get the href. How can I do it?

<span class="thumb_link">
    <a class="link_txt" href="/14803/tool/4554"> abcdef</a>
</span>

I tried the following but it failed

driver.find_element_by_css_selector('a[text()="abcdef"]')

Because the matching point is text, it must be obtained using text.

Upvotes: 0

Views: 34

Answers (2)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

The link text would do that or if you have several similar link texts. Another way is with xpath.

driver.find_element_by_xpath("//a[text()='abcdef']").get_attribute("href")

Upvotes: 1

Jimmy Suh
Jimmy Suh

Reputation: 212

Selenium's link text allows you to do that.

driver.find_element_by_link_text("abcdef").get_attribute("href")

will return the href.

Upvotes: 1

Related Questions