python - 我无法单击带有 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 后它会起作用,但它给了我错误元素不是难以处理的......还有一次它没有给我任何错误,只是一个空白异常:(
解决方案
我认为问题在于,页面中还有其他元素不可见或不可点击。也许这段代码有帮助:
download = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='appBody']//button[@class='button buttonDownload' and contains(text(),'DOWNLOAD')]")))
推荐阅读
- json - 如何修复错误不获取数据?在 swift 和 Laravel api 中
- visual-studio-code - 如何在 VS Code 的文件夹中找到短语
- ssrs-2016 - 如何将报告的标题更改为与 .rdl 文件名不同
- java - 如何根据用户输入改变活动数量?
- asp.net-core - RedisTimeoutException 使我的 aspnet 核心应用程序崩溃
- php - 使用 $_SESSION 中的数据编写 SQL 查询
- javascript - 加速 Starfield Canvas 动画并对其进行控制
- javascript - 使用 CSS 动画在页面加载后将元素隐藏 n 秒
- excel - 如何在数据表中使用宏清除行单元格值
- python - 在 ansible 上运行 pexpect 模块时,我收到消息:“需要 pexpect python 模块”