代码之家  ›  专栏  ›  技术社区  ›  Mathador

WebDriverException:消息:TypeError:rect未定义

  •  8
  • Mathador  · 技术社区  · 6 年前

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

    提前多谢!

    3 回复  |  直到 6 年前
        1
  •  9
  •   undetected Selenium    6 年前

    此错误消息。。。

    WebDriverException: Message: TypeError: rect is undefined
    

    。。。意味着 Web元素 可能没有 客户端矩形 在您尝试与之交互时定义。

    根据 TypeError: rect is undefined, when using Selenium Actions and element is not displayed. 主要问题是您试图与之交互的所需元素[即调用 click() ]是的 目前 HTML DOM 但事实上 不可见 i、 e。 未显示

    原因

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

    • 继续,当您尝试单击该元素时,所需的元素在该时间点可能无法交互,因为 JavaScript / AJAX 呼叫可能仍处于活动状态。
    • 元素超出 Viewport

    解决方案

    • 诱导 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()
      
        2
  •  8
  •   JeffC    6 年前

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

    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()
    
        3
  •  0
  •   BinaryThinker    4 年前

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

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