【发布时间】:2019-02-27 06:39:14
【问题描述】:
能否请我使用正确的 XPATH 来提取 tr id="review_" 元素? 我设法获得了元素,但幸运的是 ID,因为它们是部分匹配
<table class="admin">
<thead>"snip"</thead>
<tbody>
<tr id="review_984669" class="">
<td>weird_wild_and_wonderful_mammals</td>
<td>1</td>
<td><input type="checkbox" name="book_review[approved]" id="approved" value="1" class="attribute_toggle"></td>
<td><input type="checkbox" name="book_review[rejected]" id="rejected" value="1" class="attribute_toggle"></td>
<td>February 27, 2019 03:56</td>
<td><a href="/admin/new_book_reviews/984669?page=2">Show</a></td>
<td>
<span class="rest-in-place" data-attribute="review" data-object="book_review" data-url="/admin/new_book_reviews/984669">
bad
</span>
</td>
</tr>
<tr id="review_984670" class="striped">
我使用 Selenium 和 Chrome 来提取页面上唯一的表格。
Table_Selenium_Elements = driver.find_element_by_xpath('//*[@id="admin"]/table')
然后我使用下面的方法从每一行获取数据。
for Pri_Key, element in enumerate(Table_Selenium_Elements.find_elements_by_xpath('.//tr')):
# Create an empty secondary dict for each new Pri Key
sec = {}
# Secondary dictionary needs a Key. Keys are items in column_headers list
for counter, Sec_Key in enumerate(column_headers):
# Secondary dictionary needs Values for each key.
# Values are individual items in each sub-list of column_data list
# Slice the sub list with the counter to get each item
sec[Sec_Key] = element.get_attribute('innerHTML')[counter]
pri[Pri_Key] = sec
这只是显示每个ie中的数据 "weird_wild_and_wonderful_mammals", "1"
但我实际上也需要 tr id=review_xxx。我不知道该怎么做。 id 编号发生了变化,因此可能是 xpath 'contains' 表达式或 xpath 'begins_with' 表达式。
由于我是菜鸟,我想我已经捕获了 review_ID,但我没有通过我的 for 循环正确提取。
谁能告诉我正确的 XPATH 来提取父 tr 和子 tds。 ...然后我将调整我的 for 循环。 谢谢 山姆
【问题讨论】:
-
你能分享完整的 html 和你的代码或分享 url 吗?
-
@DaftVader 你指的是哪些元素
tr id="review_" elements?
标签: python-3.x selenium xpath