【问题标题】:Python: why driver.find_element_by_class_name gives no such element: Unable to locate elementPython:为什么 driver.find_element_by_class_name 没有给出这样的元素:无法找到元素
【发布时间】:2021-03-31 22:18:31
【问题描述】:

目前我正在使用 Selenium 尝试使用 Chrome 中的“检查”正确识别元​​素。 在某些情况下,我的某些按钮仅按类定义,但是使用''' driver.find_element_by_class_name ''' 无法找到某些类。

这是我使用 Inspect from Chrome 检索到的代码:

  <td class="x-toolbar-cell" id="ext-gen1328" role="menuitem">
  <table id="ImportMenuSmartExpReport" cellspacing="0" class="x-btn grayBtn x-btn-text-icon" role="presentation" style="width: auto;">
   <tbody class="x-btn-small x-btn-icon-small-left">
    <tr>
     <td class="x-btn-tl"><i>&nbsp;</i></td>
     <td class="x-btn-tc"></td>
     <td class="x-btn-tr"><i>&nbsp;</i></td>
    </tr>
    <tr>
     <td class="x-btn-ml"><i>&nbsp;</i></td>
     <td class="x-btn-mc"><em class="" unselectable="on"><button type="button" id="ext-gen1329" class=" x-btn-text menu_import2">Import Expenses</button></em></td>
     <td class="x-btn-mr"><i>&nbsp;</i></td>
    </tr>
    <tr>
     <td class="x-btn-bl"><i>&nbsp;</i></td>
     <td class="x-btn-bc"></td>
     <td class="x-btn-br"><i>&nbsp;</i></td>
    </tr>
   </tbody>
  </table>
  1. 我需要单击“x-btn-text menu_import2”类的“导入费用”按钮。我不能在这里使用 XPATH 或 ID,因为它是不同提交表单的动态值,它具有不同的值。 所以我尝试使用类:''' driver.find_element_by_class_name(" x-btn-text menu_import2") ''' 但是,它给了我一个错误:

Traceback(最近一次通话最后一次): 文件“”,第 1 行,在 driver.find_element_by_class_name("x-btn-text menu_import2") 文件“C:\Users\ataranov\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 564 行,在 find_element_by_class_name return self.find_element(by=By.CLASS_NAME, value=name) 文件“C:\Users\ataranov\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 976 行,在 find_element 返回 self.execute(Command.FIND_ELEMENT, { 文件“C:\Users\ataranov\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 321 行,执行 self.error_handler.check_response(响应) 文件“C:\Users\ataranov\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py”,第 242 行,在 check_response 引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:指定了无效或非法的选择器 (会话信息:chrome=89.0.4389.90

  1. 同时我可以找到'x-toolbar-cell' class :'''driver.find_element_by_class_name("x-toolbar-cell")''' 没有任何问题。
  • 用 '''find_element_by_class''' 查找我需要的类有什么问题?为什么它适用于一个班级,但不适用于另一个班级?
  • 是否与 '''x-btn-text menu_import2 ''' 包含在其他类中的事实有某种关系?如果是这样,如何查找包装到其他类的类?
  • 在这种情况下查找对象的任何其他方式,例如使用按钮文本标题“导入费用”

【问题讨论】:

  • 您所指的实际上是多个类名。您可以使用 css 选择器或使用两个类名之一来引用它。

标签: python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:
driver.find_element_by_css_selector("button.x-btn-text.menu_import2")

多个类名需要这样处理。 class_name 只处理单个类标识符。

你可以通过文字查找。

 driver.find_element_by_xpath("//button[.='Import Expenses']")

【讨论】:

  • 谢谢!确实,我可以使用您提到的两种方法找到对象。但是我无法使用 ''' click()''' 与 thouse 对象交互。在这两种情况下,它都没有说“元素不可交互”:任何提示如何进一步处理这个问题?
  • 相同的元素在通过_id 搜索时是可点击的。所以并没有真正理解为什么搜索 by_xpath 和 css_selector 将对象返回为“不可交互”,从而使 click();不工作
  • 我还注意到,对于找到 by_xpathby_css_selector 的对象,以下测试给了我 Falseprint("Element is visible? " + str(button_id.is_displayed())) 虽然找到了对象 by_id 给我 正确并且它们是可交互的
  • 尝试使用 webdriver 以使预期条件元素可点击。
猜你喜欢
  • 2021-07-07
  • 2020-07-24
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多