python - 使用 python selenium 中的任何标准定位方法都无法识别元素
问题描述
下面是我完整的 html 页面中的一个 div 部分:
<div class="x-grid3-body" style="width: 998px;" id="ext-gen159">
<div class="x-grid3-row x-grid3-row-first x-grid3-row-last" style="width:998px;">
<table class="x-grid3-row-table" border="0" cellspacing="0" cellpadding="0" style="width:998px;">
<tbody>
<tr>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column0 x-grid3-cell-first " style="width: 40px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column0" unselectable="on">
<input type="checkbox" style="display:block;float:left;margin:3px 3px 0px 0px" id="chkObjectAccountCheckbox" class="gridcheckbox" onclick="jscript">
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column1 " style="width: 0px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column1" unselectable="on">
<span class="Grid-Panel-All">
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column2 " style="width: 30px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column2" unselectable="on">
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column3 " style="width: 30px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column3" unselectable="on">
<img src="images/icons/word_lock.gif" border="0" title="The object is locked by unm" style="display:block;float:left;margin:0px 3px 0px 0px" id="">
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column4 " style="width: 30px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column4" unselectable="on">
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column5 " style="width: 187px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column5" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">runm
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column6 " style="width: 187px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column6" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">domain
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column7 " style="width: 187px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column7" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">PA-em-id-P
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column8 " style="width: 187px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column8" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">WDM-PA-
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column9 " style="width: 141px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column9" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">Roo
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column10 " style="width: 141px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column10" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">Operating System-domain-domain-domain-runm
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column11 " style="width: 141px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column11" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">PMC
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column12 " style="width: 141px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column12" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">unm
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column13 " style="width: 141px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column13" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">unm
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column14 " style="width: 141px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column14" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column15 " style="width: 141px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column15" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column16 " style="width: 141px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column16" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column17 " style="width: 141px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column17" unselectable="on" style="cursor:pointer">
<span class="Grid-Panel-All">
</span>
</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-column18 " style="width: 30px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column18" unselectable="on" style="cursor:pointer">
<img src="images/icons/word_show.gif" border="0" title="Show word" style="display:block;float:left;margin:0px 3px 0px 0px" id="">
</div>
</td>
...
...
...
<td class="x-grid3-col x-grid3-cell x-grid3-td-column28 x-grid3-cell-last " style="width: 0px;display: none; text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column28" unselectable="on">
<span class="Grid-Panel-All">
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
从上图中,橙色圈出的元素是我要自动点击的元素,而上面整个 html 片段中的部分,仅针对该元素(我的目标是下面的 img 或 div 标签识别/定位):
<td class="x-grid3-col x-grid3-cell x-grid3-td-column18 " style="width: 30px;text-align: left;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-column18" unselectable="on" style="cursor:pointer">
<img src="images/icons/word_show.gif" border="0" title="Show word" style="display:block;float:left;margin:0px 3px 0px 0px" id="">
</div>
</td>
但是当我想使用文档中的标准硒定位方法(https://selenium-python.readthedocs.io/locating-elements.html)找到元素时,没有任何效果。我还检查了,问题仅针对此 div 部分中定义的元素(整个 html 代码位于描述的开头):
html 页面之外的其余部分使用这些标准方法可以很好地工作。
我有问题的代码片段是:
def copy_pwd(self):
time.sleep(60)
#img_link = self.chrome_driver.find_element_by_xpath('//div[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img')
img_link = self.chrome_driver.find_element_by_class_name('x-grid3-cell-inner.x-grid3-col-column18')
img_link.click()
当我使用 find_element_by_xpath 时,错误是:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img"}
(Session info: chrome=85.0.4183.102)
将 xpath 从 更改//div[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img
为//*[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img
,也会给出与上述相同的异常。
当我使用 find_element_by_class_name 时,错误是:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".x-grid3-cell-inner.x-grid3-col-column18"}
(Session info: chrome=85.0.4183.102)
没有为有问题的元素定义name
, id
,link_text
因此无法使用相应的定位方法进行测试。
注意:我已经浏览了它说而不是time.sleep(60)
使用的文章,因此webdriverwait
我使用了它并进行了测试:
def copy_pwd(self):
#time.sleep(60)
find_elem = WebDriverWait(self.chrome_driver, 60).until(EC.element_to_be_clickable((By.XPATH, '//div[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img')))
if find_elem:
#img_link = self.chrome_driver.find_element_by_xpath('//div[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img')
img_link = self.chrome_driver.find_element_by_class_name('x-grid3-cell-inner.x-grid3-col-column18')
img_link.click()
但在这种情况下WebDriverWait
给了我超时异常:
find_elem = WebDriverWait(self.chrome_driver, 60).until(EC.element_to_be_clickable((By.XPATH, '//div[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img')))
File "C:\Users\user\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
后来我调查发现当元素无法识别时,使用我使用的XPATH或CLASS,超时异常WebDriverWait
也是合理的。此外,我用其他几种等待方法代替EC.element_to_be_clickable
like进行了测试EC.visibility_of_element_located
,EC.presence_of_element_located
但没有任何效果。
- 我做错了什么,为什么我不能让它工作?
- 是否有可能这部分被 html 设计者故意混淆,以阻止浏览器自动化中的任何自动化脚本?
- 我使用 XPATH 或 class_name 查找元素是否错误?
- 在这种情况下,如何利用 CSS_selector 的 find 元素?
请帮忙。
我的驱动程序是 google-chrome,版本是 85.0.4183.102。
解决方案
试试这个 xpath:
def copy_pwd(self):
#time.sleep(60)
find_elem = WebDriverWait(self.chrome_driver, 60).until(EC.presence_of_element_located((By.XPATH, '//img[@src="images/icons/word_show.gif"]')))
find_elem.click()
推荐阅读
- node.js - 节点电子商务购物车 NaN 问题
- mongodb - 在 MongoDb 中将数值与字符串进行比较
- javascript - 保存到localStorage的单一功能?
- swiftui - 无法在缩放的 ScrollView 中平移图像?
- java - 刽子手程序在正确猜到字母后不更新单词
- javascript - 如何在javascript中同时输入十进制数字和带有千位分隔符的自动格式?
- c# - 注册表 regedit 不更新值 C# 进程
- javascript - 为什么 javascript 数组允许我使用字符串作为键索引?
- javascript - 如何在循环元素和使用 addEventListener 时仅更改悬停元素的样式?
- android - 为什么应用“com.android.library”插件会破坏引用?