Selenium table 搜索未返回正确的文本
Selenium table search not returning correct text
我目前正在学习如何在 python 中使用 selenium,我有一个 table,我想检索该元素,但目前遇到了一些麻烦。
<table class="table" id="SearchTable">
<thead>..</thead>
<tfoot>..</tfoot>
<tbody>
<tr>
<td class="icon">..</td>
<td class="title">
<a class="qtooltip">
<b>I want to get the text here</b>
</a>
</td>
</tr>
<tr>
<td class="icon">..</td>
<td class="title">
<a class="qtooltip">
<b>I want to get the text here as well</b>
</a>
</td>
</tr>
</table>
在这个 table 中,我想访问粗体标记中的文本,但我的程序没有 return 正确的 tr 数,事实上我什至不确定是否它正在寻找正确的东西。
我从最后的文本回溯了我的问题,发现错误是从带有注释的行开始出现的。 (我认为之后的代码也是错误的,但我专注于首先获得正确的 table 行)
我的代码是:
search_table = driver.find_element_by_id("SearchTable")
search_table_body = search_table.find_element(By.TAG_NAME, "tbody")
trs = search_table_body.find_elements(By.TAG_NAME, "tr")
print(trs) # this does not return correct number of tr)
for tr in trs:
tds = tr.find_elements(By.TAG_NAME, "td")
for td in tds:
href = td.find_element_by_class_name("qtooltip")
print(href.get_attribute("innerHtml"))
我应该得到正确的 tr 计数,这样我就可以 return 锚标记中的文本,但我卡住了。任何帮助表示赞赏。谢谢!
你可以获得全部<b>
tags which are children of <a>
tag having class attribute of qtooltip
and living inside a table cell using a single XPath selector
//table/descendant::a[@class='qtooltip']/b
示例代码:
elements = driver.find_elements_by_xpath("//table/descendant::a[@class='qtooltip']/b")
for element in elements:
print(element.text)
演示:
参考文献:
我目前正在学习如何在 python 中使用 selenium,我有一个 table,我想检索该元素,但目前遇到了一些麻烦。
<table class="table" id="SearchTable">
<thead>..</thead>
<tfoot>..</tfoot>
<tbody>
<tr>
<td class="icon">..</td>
<td class="title">
<a class="qtooltip">
<b>I want to get the text here</b>
</a>
</td>
</tr>
<tr>
<td class="icon">..</td>
<td class="title">
<a class="qtooltip">
<b>I want to get the text here as well</b>
</a>
</td>
</tr>
</table>
在这个 table 中,我想访问粗体标记中的文本,但我的程序没有 return 正确的 tr 数,事实上我什至不确定是否它正在寻找正确的东西。
我从最后的文本回溯了我的问题,发现错误是从带有注释的行开始出现的。 (我认为之后的代码也是错误的,但我专注于首先获得正确的 table 行)
我的代码是:
search_table = driver.find_element_by_id("SearchTable")
search_table_body = search_table.find_element(By.TAG_NAME, "tbody")
trs = search_table_body.find_elements(By.TAG_NAME, "tr")
print(trs) # this does not return correct number of tr)
for tr in trs:
tds = tr.find_elements(By.TAG_NAME, "td")
for td in tds:
href = td.find_element_by_class_name("qtooltip")
print(href.get_attribute("innerHtml"))
我应该得到正确的 tr 计数,这样我就可以 return 锚标记中的文本,但我卡住了。任何帮助表示赞赏。谢谢!
你可以获得全部<b>
tags which are children of <a>
tag having class attribute of qtooltip
and living inside a table cell using a single XPath selector
//table/descendant::a[@class='qtooltip']/b
示例代码:
elements = driver.find_elements_by_xpath("//table/descendant::a[@class='qtooltip']/b")
for element in elements:
print(element.text)
演示:
参考文献: