Selenium Webdriver finding an element in a sub-element

PythonXpathSelenium

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

Use the following:

element2 = driver.find_element_by_cssselector("css=div[title='div2']")
element2.find_element_by_cssselector("p[@class='test']").text 

Please let me know if you have any problems.

Solution 3 - Python

This is how you search for element or tag in CSS subclass and I believe that it works for multilevel situation as well:

Sample HTML:

  • Posted:
  • This is how you would get pubdate tag value for example.

    published = driver.find_element_by_css_selector('li>time').get_attribute('datetime')
    

    Solution 4 - Python

    Chrome Webdriver :

    element = driver.find_element_by_id("ParentElement")
    localElement = element.find_element_by_id("ChildElement")
    print(localElement.text)
    

    Solution 5 - Python

    I guess,we need use method "By" from webdriver.common.by when use "driver.find_element".

    So...the code must be:

    from selenium import webdriver
    
    driver = webdriver.Firefox()
    from selenium.webdriver.common.by import By
    
    element2 = driver.find_element(By.XPATH, "//div[@title='div2']")
    element2.find_element(By.XPATH, ".//p[@class='test']").text
    
    

    Attributions

    All content for this solution is sourced from the original question on Stackoverflow.

    The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

    Content TypeOriginal AuthorOriginal Content on Stackoverflow
    QuestionDominic LarkinView Question on Stackoverflow
    Solution 1 - Pythonp0dejeView Answer on Stackoverflow
    Solution 2 - Pythonuser3487861View Answer on Stackoverflow
    Solution 3 - PythonHrvojeView Answer on Stackoverflow
    Solution 4 - PythonsvvcView Answer on Stackoverflow
    Solution 5 - Pythonefraim.limaView Answer on Stackoverflow