Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get link from elements with Selenium and Python

Let's say all Author/username elements in one webpage look like this... How can I get to the href part using python and Selenium? users = browser.find_elements_by_xpath(?)

<span>

    Author: 

    <a href="/account/57608-bob">

        bob

    </a>

</span>

Thanks.

like image 842
jacob501 Avatar asked Dec 31 '13 01:12

jacob501


People also ask

How do you find the href of an element?

Use the querySelector() method to get an element by an href attribute, e.g. document. querySelector('a[href="https://example.com"]') . The method returns the first element that matches the selector or null if no element with the provided selector exists in the DOM.

What is link text in Selenium Python?

Selenium Web DriverAutomation TestingSoftware Testing. We can click on a link on page with the help of locators available in Selenium. Link text and partial link text are the locators generally used for clicking links. Both these locators work with the text available inside the anchor tags.


1 Answers

Use find_elements_by_tag_name('a') to find the 'a' tags, and then use get_attribute('href') to get the link string.

like image 154
WKPlus Avatar answered Oct 06 '22 20:10

WKPlus