使用 selenium 获取 td class 文本
Get td class text with selenium
所以我想取td的文字class.
html 页面
<table class="table table-striped">
<tbody>
<tr>
<td class="text-center">
<img .....>
</td>
<td>text</td>
<td>text</td>
<td class="text-center">
<a ....></a>
</td>
<td class="text-center">
TEXT I WANT TO TAKE HERE
</td>
<td class="text-center">
<a ....><i class="fa fa-times"></i></a>
</td>
</tr>
</tbody>
</table>
我要取的文字是"TEXT I WANT TO TAKE HERE"。
我尝试使用如下所示的 xpath,但它没有用
table = browser.find_element_by_xpath(("//div[@class='table table-striped']/tbody/tr/td[5]"));
我收到一条错误消息:
no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='table table-striped']/tbody/tr/td[5]"}
是不是因为我的选择器里有多个class所以必须用点?
(我试过了:'table.table-striped' 但还是不行)
在您的 XPath 表达式中,您正在寻找 div 标记,但您的 HTML 没有。也许您正在查看 table 标签:
table = browser.find_element_by_xpath(("//table[@class='table table-striped']/tbody/tr/td[5]"));
使用下面的 xpath 获取文本
browser.find_element_by_xpath("//td[@class='text-center']").text
并使用索引更好地找到您的行,例如
browser.find_element_by_xpath("//td[@class='text-center'][3]").text
您的 xpath 不正确。您有一个 table
标签,但您正在寻找 div
标签。因此,您只需将 div
替换为 table
.
table = browser.find_element_by_xpath(("//table[@class='table table-striped']/tbody/tr/td[5]"));
使用下方 xpath
获取文本 TEXT I WANT TO TAKE HERE
//table//tr/td[contains(text(), 'TEXT I WANT TO TAKE HERE')]
更新答案:您可以参考下面提到的任何 xpath 来获取您的网络元素。
//td[5]
OR
//table[@class='table table-striped']//td[5]
OR
//table[@class='table table-striped']/..//following-sibling::td[5]
OR
//td[@class='text-center'][3]
所以我想取td的文字class.
html 页面
<table class="table table-striped">
<tbody>
<tr>
<td class="text-center">
<img .....>
</td>
<td>text</td>
<td>text</td>
<td class="text-center">
<a ....></a>
</td>
<td class="text-center">
TEXT I WANT TO TAKE HERE
</td>
<td class="text-center">
<a ....><i class="fa fa-times"></i></a>
</td>
</tr>
</tbody>
</table>
我要取的文字是"TEXT I WANT TO TAKE HERE"。
我尝试使用如下所示的 xpath,但它没有用
table = browser.find_element_by_xpath(("//div[@class='table table-striped']/tbody/tr/td[5]"));
我收到一条错误消息:
no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='table table-striped']/tbody/tr/td[5]"}
是不是因为我的选择器里有多个class所以必须用点? (我试过了:'table.table-striped' 但还是不行)
在您的 XPath 表达式中,您正在寻找 div 标记,但您的 HTML 没有。也许您正在查看 table 标签:
table = browser.find_element_by_xpath(("//table[@class='table table-striped']/tbody/tr/td[5]"));
使用下面的 xpath 获取文本
browser.find_element_by_xpath("//td[@class='text-center']").text
并使用索引更好地找到您的行,例如
browser.find_element_by_xpath("//td[@class='text-center'][3]").text
您的 xpath 不正确。您有一个 table
标签,但您正在寻找 div
标签。因此,您只需将 div
替换为 table
.
table = browser.find_element_by_xpath(("//table[@class='table table-striped']/tbody/tr/td[5]"));
使用下方 xpath
获取文本 TEXT I WANT TO TAKE HERE
//table//tr/td[contains(text(), 'TEXT I WANT TO TAKE HERE')]
更新答案:您可以参考下面提到的任何 xpath 来获取您的网络元素。
//td[5]
OR
//table[@class='table table-striped']//td[5]
OR
//table[@class='table table-striped']/..//following-sibling::td[5]
OR
//td[@class='text-center'][3]