Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver finding an element in a sub-element

I am trying to search for an element in a sub-element with Selenium (Version 2.28.0), but selenium des not seem to limit its search to the sub-element. Am I doing this wrong or is there a way to use element.find to search a sub-element?

For an example I created a simple test webpage with this code:

<!DOCTYPE html> <html>     <body>         <div class=div title=div1>             <h1>My First Heading</h1>             <p class='test'>My first paragraph.</p>         </div>         <div class=div title=div2>             <h1>My Second Heading</h1>             <p class='test'>My second paragraph.</p>         </div>         <div class=div title=div3>             <h1>My Third Heading</h1>             <p class='test'>My third paragraph.</p>         </div>     </body> </html> 

My python (Version 2.6) code looks like this:

from selenium import webdriver  driver = webdriver.Firefox()  # Open the test page with this instance of Firefox  # element2 gets the second division as a web element element2 = driver.find_element_by_xpath("//div[@title='div2']")  # Search second division for a paragraph with a class of 'test' and print the content print element2.find_element_by_xpath("//p[@class='test']").text  # expected output: "My second paragraph." # actual output: "My first paragraph." 

If I run:

print element2.get_attribute('innerHTML') 

It returns the html from the second division. So selenium is not limiting its search to element2.

I would like to be able to find a sub-element of element2. This post suggests my code should work Selenium WebDriver access a sub element but his problem was caused by a time-out issue.

Can anyone help me understand what is happening here?

like image 216
Dominic Larkin Avatar asked Dec 27 '12 05:12

Dominic Larkin


People also ask

How do I find an element that contains specific text in Selenium Webdriver?

We can find an element that contains specific text with Selenium webdriver in Python using the xpath. This locator has functions that help to verify a specific text contained within an element. The function text() in xpath is used to locate a webelement depending on the text visible on the page.

What is findElement () in Selenium?

findElement method in Selenium is a command which helps you identify a web element. There are multiple ways that findElement provides to uniquely identify a web element within the web page using web locators in Selenium like ID, Name, Class Name, Link Text, Partial Link Text, Tag, which we will see later in the blog.

Which is the best way to locate an element in Selenium?

ID locator in Selenium is the most preferred and fastest way to locate desired WebElements on the page. ID Selenium locators are unique for each element in the DOM. Since IDs are unique for each element on the page, it is considered the fastest and safest method to locate elements.


1 Answers

If you start an XPath expression with //, it begins searching from the root of document. To search relative to a particular element, you should prepend the expression with . instead:

element2 = driver.find_element_by_xpath("//div[@title='div2']") element2.find_element_by_xpath(".//p[@class='test']").text 
like image 62
p0deje Avatar answered Oct 05 '22 22:10

p0deje