Selenium不会点击,无名称,ID或类
我想selenium webdriver点击这个链接,所以我可以继续我的项目。这是我的代码。没有错误,但没有点击链接。Selenium不会点击,无名称,ID或类
x = driver.find_element_by_xpath('/html/body/table[2]/tbody/tr[2]/td/span/ a[7]')
x.click()
下面是HTML
<tr> <td width="100%">
<span style="white-space: nowrap">
<a href="#">
<img src="/wp-content/uploads/new2022/20220327voicc/106152400.gif" border="0" width="125" height="36">
</a>
<a href="#">
<img src="/wp-content/uploads/new2022/20220327voicc/107152402.gif" border="0" width="125" height="36">
</a>
<a href="#">
<img src="/wp-content/uploads/new2022/20220327voicc/109152403.gif" border="0" width="125" height="36">
</a>
<a href="#">
<img src="/wp-content/uploads/new2022/20220327voicc/110152405.gif" border="0" width="125" height="36">
</a>
<a href="#">
<img src="/wp-content/uploads/new2022/20220327voicc/112152408.gif" border="0" width="125" height="36">
</a>
<a href="#">
<img src="/wp-content/uploads/new2022/20220327voicc/113152409.gif" border="0" width="125" height="36">
</a>
<a href="eprintreports.php?eSessID=199495633810875522501415473608154261624185901388039708769020171218111124">
<img src="/wp-content/uploads/new2022/20220327voicc/114152416.gif" border="0" width="125" height="36">
</a>
</span>
</td>
</tr>
我想点击链接第七。我从Chrome的检测工具中直接复制了xpath。我知道一个绝对的xpath是不可靠的,但是当没有类名,id或者名字时我还有什么可用的?帮助将不胜感激。
回答:
尝试以下XPath:在<a>
标签
"//a[contains(@src, 'PREP')]"
回答:
点击可能无法获取您所需的输出。你需要去更深,直到<img>
标签并调用click()
如下:
driver.find_element_by_xpath("//td/span//a/img[contains(@src,'https://staffmate.com/imagessm/PREP_sm_lt.gif')]").click()
Note : We could have done away with a shorter value for
src
attribute asPREP
but ideally we should consider the fullvalue
of the attribute.
以上是 Selenium不会点击,无名称,ID或类 的全部内容, 来源链接: utcz.com/qa/261144.html