How can I get the href in a <a class in Python Selenium?


I am trying to get a href from a webpage with selenium. When I use the browser console with following javascript command I get the right href:

Using Selenium trying to do the same, looking like this:


I get the error code:

AttributeError: 'WebElement' object has no attribute 'getattr'

The HTML code:

<a class="item-panel__title" href="/u/abcd" rel="">@abcd</a>
<div class="item-panel__description">abcd</div>

Asked By: Winters Long



Getting web element attribute value with Selenium in Python is done with get_attribute() method.
So, instead of handle_browser.find_elements(By.CLASS_NAME,'item-panel__title')[0].getattr('href')
try using


Also, since you are getting from the first element in the list of possible matchings, you can use find_element instead of find_elements, as following:

Answered By: Prophet