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"
.
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.
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.
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.
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.
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, ...
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With