Skip to content Skip to sidebar Skip to footer

How Do I Find Node And Its Child Nodes Using Selenium With Python

Please let me know the code syntax how to use Node.childNodes from the documentation http://docs.python.org/library/xml.dom.html#module-xml.dom I am beginner to python and selenium

Solution 1:

elem is WebElement. You could use elem.find_elements_by_xpath() to select relevant child elements e.g.:

#!/usr/bin/env python
from contextlib import closing

from selenium.webdriver import Chrome as Browser # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait

with closing(Browser()) as browser:
    browser.get('http://stackoverflow.com/q/9548523')
    elem = WebDriverWait(browser, timeout=10).until(
        lambda br: br.find_element_by_class_name('related'))
    children = elem.find_elements_by_xpath('./*')
    for child in children:
        print("<%s> %r" % (child.tag_name, child.text[:60]))

Output

<div> u'How to handle dialog box through selenium with python?'
<div> u'Networkx node traversal'
<div> u'How to set up Selenium to work with Visual Studio .NET using'
<div> u'How can I find text location with Selenium?'
<div> u"Using Selenium's Python API - How do I get the number of row"
<div> u'Selenium in Python'
...[snip]...

Post a Comment for "How Do I Find Node And Its Child Nodes Using Selenium With Python"