WebDriverException: Message: TypeError: rect is undefined

WebDriverException: Message: TypeError: rect is undefined

我正在尝试使用 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 个小时没有任何结果。因此欢迎任何想法!

提前致谢!

这个错误信息...

WebDriverException: Message: TypeError: rect is undefined

...意味着当您尝试与之交互时,所需的 WebElement 可能没有定义 client rects

根据 TypeError: rect is undefined, when using Selenium Actions and element is not displayed. the main issue is though the desired element with which you are trying to interact [i.e. invoke click()] is present within the HTML DOM 不可见 不显示 .

原因

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

  • 在您尝试单击该元素时继续前进,所需的元素在那个时间点可能无法交互,因为某些 JavaScript / Ajax 通话可能仍在进行中。
  • 元素不在 Viewport

解决方案

  • Induce 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()
    
  • 使用execute_script()方法将元素滚动进去查看如下:

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

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

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

我也有这个问题。 但是当我编写如下代码时,我没有任何问题。

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