xpath

时间:2018-02-20 18:16:45

标签: selenium xpath xpath-1.0

无法获取匹配某个类的所有匹配项的文本,无论它出现在何处......

name = chrome_session.find_element_by_xpath('//*[@class="u-title-font u-c-pointer u-bold"]').text

目前的输出:Shyam Sundar Reddy P博士

示例html

<div><h2 data-qa-id="doctor_name" class="u-title-font u-c-pointer u-bold" data-reactid="543">Dr. Shyam Sundar Reddy P</h2></div>
<div><h2 data-qa-id="doctor_name" class="u-title-font u-c-pointer u-bold" data-reactid="650">Dr. C.H. Umesh Chandra</h2></div>

期望输出为

Dr. Shyam Sundar Reddy P,
Dr. C.H. Umesh Chandra

等等

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

尝试获取以下值列表:

names = [node.text for node in chrome_session.find_elements_by_xpath('//*[@class="u-title-font u-c-pointer u-bold"]')]

请注意,虽然XPath //*[@class="u-title-font u-c-pointer u-bold"]返回节点列表,但find_element_by_xpath()打算只返回第一次出现的节点。您需要find_elements_by_xpath()才能获得所有出现次数