Extract link from XPath using Selenium Webdriver and Python?


Question

I'm rather new to Seleniun WebDriver and Python and my question might be kind of basic.

So, I have the following HTML code:

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

And I am trying to extract the href out of it, be means of XPath, knowing that its XPath is ".//*[@id='toplevel_page_menu']/ul/li[2]/a".

How do I do that?

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

Do not seem to work, resulting in:

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

I'm expecting the result to be like "admin.php?page=account".

1
7
3/13/2013 2:26:03 PM

Accepted Answer

You could use get_attribute:

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

Ordinarily I use Selenium to navigate to a page, retrieve 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']

Unfortunately, BeautifulSoup does not support xpath, so the above is a BS representation of your xpath (as far as I understand it).

11
3/13/2013 4:12:01 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon