Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用selenium和python查找节点及其子节点_Python_Selenium_Nodes - Fatal编程技术网

如何使用selenium和python查找节点及其子节点

如何使用selenium和python查找节点及其子节点,python,selenium,nodes,Python,Selenium,Nodes,请让我从文档中了解如何使用Node.childNodes的代码语法 我是python和selenium的初学者。我试过使用: elem = self.browser.find_element_by_id("pie4") x = elem.childNodes print x 我也尝试过: self.dom.getElementsByTagName('path')[0].firstChild.data 但两者都失败了。elem是。您可以使用elem.find_elements_by_xpat

请让我从文档中了解如何使用Node.childNodes的代码语法 我是python和selenium的初学者。我试过使用:

elem = self.browser.find_element_by_id("pie4")
x = elem.childNodes
print x 
我也尝试过:

self.dom.getElementsByTagName('path')[0].firstChild.data
但两者都失败了。

elem是。您可以使用elem.find_elements_by_xpath选择相关的子元素,例如:

#!/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]))
输出
代码如下所示:感谢您的上述建议。我尝试过,对于循环,对于id为find_element_by_idpie4,我得到一个错误webelement不可编辑。@user1117498:您可以将您的问题包含在正确格式的xml中
<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]...