Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

python 3.x - WebDriverException: Message: TypeError: rect is undefined

I am trying to automate the download of data from a website with a python script using selenium but I get the following error:

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

Code trial:

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)

Now I define the check-box I want to click and I try to click on it:

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

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

I already searched 2 hours on the net without any success. Any idea is therefore welcome!

Thanks a lot in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...