【发布时间】:2020-08-04 08:49:00
【问题描述】:
我尝试使用 selenium 和 python 来自动化一些任务。该程序将通过单击链接javascript:LoadPage('12') 来导航它。但是,我不能用硒来做到这一点。我的代码如下,任何建议请帮助。
webdriver.find_element_by_partial_link_text('Security')
webdriver.find_element_by_link_text("javascript:LoadPage('12')")
webdriver.find_element_by_link_text('Security')
其中三个是我尝试过但不起作用的代码。我有一个错误 liMessage: no such element: Unable to locate element: {"method":"partial link text","selector":"Security"}
提前谢谢你。
Edit1:添加html代码
<table height="40px" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td width="90px" height="40px" bgcolor="#d8d8d8">
<a href="javascript:LoadPage('0')" target="" class="Menu_L1_Link">
<p align="center">Status</p>
</a>
</td>
<td width="90px" height="40px" bgcolor="#d8d8d8">
<a href="javascript:LoadPage('6')" target="" class="Menu_L1_Link">
<p align="center">Network</p>
</a>
</td>
<td width="90px" height="40px" bgcolor="#FFC000">
<a href="javascript:LoadPage('12')" target="" class="Menu_L1_Active">
<p align="center">Security</p>
</a>
</td>
<td width="90px" height="40px" bgcolor="#d8d8d8">
<a href="javascript:LoadPage('19')" target="" class="Menu_L1_Link">
<p align="center">Application</p>
</a>
</td>
<td width="90px" height="40px" bgcolor="#d8d8d8">
<a href="javascript:LoadPage('26')" target="" class="Menu_L1_Link">
<p align="center">Management</p>
</a>
</td>
</tr>
</tbody>
</table>
【问题讨论】:
-
试试
webdriver.find_element('.Menu_L1_Link')。想法是有一个选择器。如果您需要精确,请尝试.find_element(' a.Menu+L1_Link[href="javascript:LoadPage('12')"] ') -
@Rajesh 它不起作用,因为
class="Menu_L1_Link中包含 5 个子菜单并出现.find_element(' a.Menu+L1_Link[href="javascript:LoadPage('12')"] ')的语法错误 -
@SasiwutChaiyadecha 你能把你的图片发布为html吗?
-
@frianH 已添加,谢谢
-
@SasiwutChaiyadecha 试试
webdriver.find_element_by_css_selector('table > tbody > tr > td:nth-child(3) > a').click()
标签: python selenium xpath css-selectors webdriverwait