Extract link from XPath using Selenium Webdriver and Python?

I am new to Seleniun WebDriver and Python, and my question can be quite simple.

So, I have the following HTML code:

<a class="wp-first-item" href="admin.php?page=account">Account</a>

And I'm trying to extract href from it, to be an XPath tool, knowing that its XPath ".//*[@id='toplevel_page_menu']/ul/li[2]/a".

How should I do it?

driver.find_element_by_xpath(".//*[@id='toplevel_page_menu']/ul/li[2]/a").link

or

driver.find_element_by_xpath(".//*[@id='toplevel_page_menu']/ul/li[2]/a").href

It doesn't seem to work, resulting in:

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

I expect the result to be like "admin.php?page=account".

+5
source share
1 answer

You can use get_attribute:

element = driver.find_element_by_xpath(".//*[@id='toplevel_page_menu']/ul/li[2]/a")
href = element.get_attribute('href')
print href

I usually use Selenium to go to the page, extract the source and parse it with BeautifulSoup :

from BeautifulSoup import BeautifulSoup

# On the current page
source = driver.page_source
soup = BeautifulSoup(source)

href = soup('<the tag containing the anchor>',{'id':'toplevel_page_menu'})[0]('ul')[0]('li')[2]('a')[0]['href']

, BeautifulSoup xpath, BS xpath ( ).

+6

All Articles