【问题标题】:Click on element is present not working : error (AttributeError: 'str' object has no attribute 'presence_of_element_located')单击元素不存在:错误(AttributeError:'str'对象没有属性'presence_of_element_located')
【发布时间】:2020-01-24 21:23:34
【问题描述】:

我想在元素存在时点击它,但我收到以下错误消息:

AttributeError: 'str' object has no attribute presence_of_element_located'

这是我在 Pycharm 中的代码:

try:
    element = WebDriverWait(self.driver, 120).until(EC.presence_of_element_located(By.XPATH, '/html/body/div[1]/section/md-content/div/div/div/div[2]/div/div/div/div/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[1]/div/section[1]/h2/button[1]'))
    element.click()
finally:
    self.driver.quit()

【问题讨论】:

  • 显然EC 是一个字符串。如果这不是您所期望的,则需要进行一些调试。但是,您没有提供足够的信息让我们能够为您提供帮助。
  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您指定的问题。

标签: python selenium pycharm


【解决方案1】:

检查是否将预期条件导入为

from selenium.webdriver.support import expected_conditions as EC 

然后没有覆盖EC变量,例如

EC = "//div"

...什么的

【讨论】:

  • 现在它给了我这个“/html/body/div[1]/section/md-content/div/div/div/div[2]/div/”的错误(意外参数) div/div/div/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[1]/div/section[1]/h2/button[1]"
  • @FlutterLover ,尝试再添加一对括号:EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/section/md-content/div/div/div/div[2]/div/div/div/div/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[1]/div/section[1]/h2/button[1]'))
  • 点击时出现此错误:../../../../../../../venv/lib/python2.7/site-packages/selenium/ webdriver/remote/webelement.py:80: 在点击 self._execute(Command.CLICK_ELEMENT) ../../../../../../../venv/lib/python2.7/site -packages/selenium/webdriver/remote/webelement.py:633: 在 _execute 中返回 self._parent.execute(command, params) ../../../../../../../venv /lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py:321:在执行self.error_handler.check_response(response)
  • @FlutterLover ,我只看到部分异常,它根本没有给我任何有用的信息((
  • 我终于找到了一个解决方案 element = self.driver.find_element_by_id('adminCreateConsultation') self.driver.execute_script("arguments[0].click();", element)
猜你喜欢
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多