how do i find node and its child nodes using selenium with python

纵饮孤独 提交于 2019-12-13 03:41:37

问题


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. I have tried using:

elem = self.browser.find_element_by_id("pie4")
x = elem.childNodes
print x 

I have also tried:

self.dom.getElementsByTagName('path')[0].firstChild.data

But both fail.


回答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]...


来源:https://stackoverflow.com/questions/9548523/how-do-i-find-node-and-its-child-nodes-using-selenium-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!