Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract link from XPath using Selenium Webdriver and Python?

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".

like image 754
Robin Molnar Avatar asked Mar 13 '13 14:03

Robin Molnar


People also ask

How do you select a link in Selenium Python?

We can click on a link using Selenium webdriver in Python. A link is represented by the anchor tag. A link can be identified with the help of the locators like - link text and partial link text. We can use the link text attribute for an element for its identification and utilize the method find_element_by_link_text.

How do you select a link in Selenium?

We can click a link by href value with Selenium webdriver. To identify the link with the href attribute we can take the help of the locators xpath or css selector. We shall use the findElement method and pass By. xpath or By.

What is XPath in Selenium WebDriver?

XPath, also known as XML Path, is one of the most commonly used locators in Selenium WebDriver that can help you navigate through the HTML structure of a page. It can be used for HTML and XML documents to locate any element in a web page using HTML DOM structure.

How to use selenium Python with Selenium WebDriver?

Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. After you have installed selenium and checked out – Navigating links using get method, you might want to play more with Selenium Python.

What is find_element_by_XPath driver method in selenium Python?

find_element_by_xpath () driver method – Selenium Python. Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. After you have installed selenium and checked out – Navigating links using get method, ...

What are the axes methods in Selenium WebDriver?

There are few axes methods commonly used in Selenium Webdriver like child, parent, ancestor, sibling, preceding, self, etc. XPath expression select nodes or list of nodes on the basis of attributes like ID , Name, Classname, etc. from the XML document as illustrated below.


1 Answers

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).

like image 97
That1Guy Avatar answered Sep 28 '22 19:09

That1Guy