简体   繁体   English

使用 python selenium 中的任何标准定位方法无法识别元素

[英]Element is not identifiable using any standard locating methods in python selenium

Below is a div section from my complete html page:下面是我完整的 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">&nbsp;
      </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">&nbsp;
      </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>

For the above snippet , Please have a look at the image below to get the idea of the structure of the div:对于上面的代码片段,请查看下图以了解 div 的结构: 来自上面 html 片段的 div 部分

From the image above, the element circled in orange is the one I'm targeting to automate clicking, and the section from the whole html snippet above, just for that element is (I'm targeting either the img or div tag below to be identified/located):从上图中,用橙色圈起来的元素是我的目标是自动点击的元素,以及上面整个 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>

But when I want to find the element using standard selenium locating methods from the documentation( https://selenium-python.readthedocs.io/locating-elements.html ), nothing works.但是,当我想使用文档( https://selenium-python.readthedocs.io/locating-elements.html )中的标准硒定位方法查找元素时,没有任何效果。 Also I checked, the issue is for only the elements defined in this div section(for which the whole html code is at the beginning of the description):我还检查过,问题仅针对此 div 部分中定义的元素(整个 html 代码位于描述的开头):

Rest of the part of the html page outside of that works pretty fine using those standard methods.使用这些标准方法,html 页面之外的其余部分可以很好地工作。

My problematic code snippet is:我有问题的代码片段是:

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()

When I use find_element_by_xpath, error is:当我使用 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)

Changing the xpath from //div[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img to //*[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img , also gives same exception as above.将 xpath 从//div[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img更改为//*[@id="ext-gen159"]/div/table/tbody/tr/td[19]/div/img ,也给出与上面相同的例外。

When I use find_element_by_class_name, error is:当我使用 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)

There is no name , id , link_text defined for the element in question, so couldn't use the respective location methods to test.没有为相关元素定义nameidlink_text ,因此无法使用各自的位置方法进行测试。

NOTE: I've already gone through articles where it says instead of time.sleep(60) use webdriverwait and I hence used this instead and tested as well:注意:我已经阅读了一些文章,它说而不是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()

But in this case WebDriverWait gives me timeout exception:但在这种情况下, 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:

Later I investigated and found when the element is not identifiable, at all, using the XPATH or CLASS that I used, timeout exception by the WebDriverWait is also justifiable.后来查了查,当元素根本无法识别时,使用我使用的XPATH或CLASS, WebDriverWait超时异常也是有道理的。 Also I did the test with several other wait methods in place of EC.element_to_be_clickable like EC.visibility_of_element_located and EC.presence_of_element_located but nothing works.我还使用其他几种等待方法代替EC.element_to_be_clickable进行了测试,例如EC.visibility_of_element_locatedEC.presence_of_element_located但没有任何效果。

  1. What am I doing wrong, and why am I not able to make it work ?我做错了什么,为什么我不能让它工作?
  2. Is it possible that this section is intentionally obfuscated by the html designer, to refrain any automated script from browser automation ? html 设计者是否有意混淆了此部分,以防止浏览器自动化中的任何自动化脚本?
  3. Is my use of XPATH or class_name for find element wrong ?我使用 XPATH 或 class_name 查找元素是否错误?
  4. How can I make use of find element by CSS_selector in that case ?在这种情况下,如何通过 CSS_selector 使用 find 元素?

Please help.请帮忙。

My driver is google-chrome and version is 85.0.4183.102.我的驱动程序是 google-chrome,版本是 85.0.4183.102。

Try this xpath:试试这个 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()

This will search all divs class which contains 'x-grid3-col-column18' text.这将搜索包含“x-grid3-col-column18”文本的所有 div 类。 After find divs get one under level img elements.找到 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()

This will search first div class which contains 'x-grid3-col-column18' text.这将搜索包含“x-grid3-col-column18”文本的第一个 div 类。 After find divs' child img elements which title is 'Show word'.在找到标题为“显示单词”的 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()

For more detailed examples check this link: https://towardsdatascience.com/xpath-for-python-89f4423415e0有关更详细的示例,请查看此链接: https : //towardsdatascience.com/xpath-for-python-89f4423415e0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM