首页 > 解决方案 > 我无法单击带有 selenium 的元素,它给了我错误元素不可交互

问题描述

我正在尝试制作电影报价制作机器人,并且我已经从 IMDB 制作了随机报价刮板。但是当我尝试从 www.quotescover.com 引用时,我的 selenium 出现错误

我已经尝试了很多隐式点击,使用 time.sleep 甚至尝试使用我在代码中提到的动作链

qt,mv = getQuote() #basically the quote and the movie name, it can be just any string like qt = 'foo';mv = 'bar'
url = 'https://quotescover.com/designs/wording?res=WTBTQ3FyR01aMEVOdTVCRVJxRFUwdz09'
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
# options.add_argument("--headless")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
prefs = {'download.default_directory' : os.getcwd()+'images\\'}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options=options)
driver.get(url)
quote = driver.find_element_by_id('quotes')
quote.clear()
quote.send_keys(qt)
movie = driver.find_element_by_id('author')
movie.clear()
movie.send_keys(mv)
submit = driver.find_element_by_id('template-contactform-submit')
submit.click()

download = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'button  buttonDownload')))
for item in download:
    ActionChains(driver).move_to_element(item).click().perform()

我认为在使用 actionchains 后它会起作用,但它给了我错误元素不是难以处理的......还有一次它没有给我任何错误,只是一个空白异常:(

标签: pythonpython-3.xseleniumselenium-webdriverselenium-chromedriver

解决方案


我认为问题在于,页面中还有其他元素不可见或不可点击。也许这段代码有帮助:

download = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='appBody']//button[@class='button  buttonDownload' and contains(text(),'DOWNLOAD')]")))

推荐阅读