WebDriverException:消息:类型错误:矩形未定义

新手上路,请多包涵

我正在尝试使用 selenium 使用 python 脚本自动从网站下载数据,但出现以下错误:

 "WebDriverException: Message: TypeError: rect is undefined".

代码试用:

 from selenium import webdriver
from selenium.webdriver.common import action_chains

driver = webdriver.Firefox()
url="https://www.hlnug.de/?id=9231&view=messwerte&detail=download&station=609"
driver.get(url)

现在我定义要单击的复选框并尝试单击它:

 temp=driver.find_element_by_xpath('//input[@value="TEMP"]')
action = action_chains.ActionChains(driver)

action.move_to_element(temp)
action.click()
action.perform()

我已经在网上搜索了 2 个小时,但没有成功。因此欢迎任何想法!

非常感谢!

原文由 Mathador 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 717
2 个回答

有两个元素匹配该定位器。第一个是不可见的,所以我假设你想点击第二个。

 temp = driver.find_elements_by_xpath('//input[@value="TEMP"]')[1] # get the second element in collection
action = action_chains.ActionChains(driver)

action.move_to_element(temp)
action.click()
action.perform()

原文由 JeffC 发布,翻译遵循 CC BY-SA 4.0 许可协议

这个错误信息…

 WebDriverException: Message: TypeError: rect is undefined

…意味着当您尝试与之交互时,所需的 WebElement 可能没有定义 客户端矩形

根据 TypeError: rect is undefined, when using Selenium Actions and element is not displayed. 主要问题是尽管您尝试与之交互的所需元素 [即调用 click() ] 存在HTML DOM 中,但 不可见,即 不显示

原因

最可能的原因和解决方法如下:

  • 当您尝试单击该元素时继续前进,所需的元素在那个时间点可能无法交互,因为某些 JavaScript / Ajax 调用可能仍处于活动状态。
  • 元素超出 视口

解决方案

  • 诱导 WebDriverWait 元素可点击,如下所示:
   temp = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@value="TEMP"]")))
  action = action_chains.ActionChains(driver)
  action.move_to_element(temp)
  action.click()
  action.perform()

   temp = driver.find_element_by_xpath("//input[@value="TEMP"]")
  driver.execute_script("arguments[0].scrollIntoView();", temp);
  action = action_chains.ActionChains(driver)
  action.move_to_element(temp)
  action.click()
  action.perform()

原文由 undetected Selenium 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进