【发布时间】:2019-05-12 23:56:02
【问题描述】:
我正在尝试从一个没有 Id 的表中获取数据,我在使用 Python 和 Selenium 时似乎无法搜索。下面是 HTML 的 sn-p,我正在尝试获取名称,名称后面还有其他行,但我只是在这里显示名称。有什么想法可以让我遍历所有这些条目并获取它们的值。
我尝试了一些搜索 Div 类的示例,但无法使其正常工作。下面是我试图搜索的原始 HTML。
<div class="block-content-body clear-block">
<div class="inline-block" style="border-top:2px solid #50afb1; padding:0px;">
<div class="table-block standard no-plumb-line">
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td id="" style="padding-left:4px;padding-bottom:2px;height:25px;vertical-align:middle">
<div>
Name</div>
</td>
<td id="" class="text-center" style="padding-left:4px;padding-bottom:2px;height:25px;vertical-align:middle">
<div>
<strong>
David
</strong>
</div>
</td>
</tr>
我想获取名称字段和值大卫。像地址行 1 和 2 等下还有其他字段。我想得到所有这些。
我的代码如下:
elements = driver.find_elements_by_xpath("//div[@class='table-block standard no-plumb-line']")
print(elements)
for num in (1,elements.length):
text1 = driver.find_element_by_xpath("//*[@class='table-block standard no-plumb-line']/div["+num+"]/div[1]").text
text2 = driver.find_element_by_xpath("//*[@class='table-block standard no-plumb-line']/div["+num+"]/div[2]").text
print(text1)
print(text2)
【问题讨论】: