WebDriverException: Message: TypeError: rect is undefined

旧街凉风 提交于 2019-12-02 07:22:41

There are two elements that match that locator. The first one is not visible so I'm assuming you want to click on the second.

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

This error message...

WebDriverException: Message: TypeError: rect is undefined

...implies that the desired WebElement might not have client rects defined when you tried to interact with it.

As per 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 but is not visible i.e. not displayed.

Reason

The most probhable reasons and solutions are as follows :

  • Moving ahead as you are trying to click the element, the desired element may not be interactable at that point of time as some JavaScript / Ajax call may be still active.
  • Element is out of the Viewport

Solution

  • Induce WebDriverWait for the element to be clickable as follows :

    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()
    
  • Use execute_script() method to scroll the element in to view as follows :

    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()
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!