[英]Element is not identifiable using any standard locating methods in 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
進行了測試,例如EC.visibility_of_element_located
和EC.presence_of_element_located
但沒有任何效果。
請幫忙。
我的驅動程序是 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()
這將搜索包含“x-grid3-col-column18”文本的所有 div 類。 找到 div 后,在 img 元素下獲得一個。
find_elems = WebDriverWait(self.chrome_driver, 60).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@class,'x-grid3-col-column18')]/img")))
find_elems[0].click()
這將搜索包含“x-grid3-col-column18”文本的第一個 div 類。 在找到標題為“顯示單詞”的 div 的子 img 元素之后。
find_elem = WebDriverWait(self.chrome_driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'x-grid3-col-column18')]//img[@title='Show word']")))
find_elem.click()
有關更詳細的示例,請查看此鏈接: https : //towardsdatascience.com/xpath-for-python-89f4423415e0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.