消息:尝试通过Selenium和Python单击复选框时,元素不可见 [英] Message: element not visible when tring to click a checkbox through Selenium and Python

查看:335
本文介绍了消息:尝试通过Selenium和Python单击复选框时,元素不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我一直在从事的项目的一部分自动化. 项目的这一部分应进入页面并获取随机生成的密钥. 要获取密钥,请转至网页,登录,然后按一些按钮以获取新密钥. 我一直在使用Selenium和Chrome驱动程序.

I am trying to automate one part of my project I've been working on. This part of the project should enter a page and get a randomly generated key. To get the key it goes to the web-page, login and then presses some buttons to get the new key. I've been using Selenium with Chrome driver to do so.

driver = webdriver.Chrome()

当我必须选中某些复选框时,问题就开始了.

The problem started when I had to check some checkboxes.

页面的外观如下: https://imgur.com/a/p3kmklT

源文件看起来像其JavaScript一样.

Source file looks like its JavaScript rendered.

到目前为止我尝试过的是: 通过ID获取它:

What I've tried so far: Getting it by id:

checkbox = label.find_element_by_id("agreed")
checkbox.click()

通过XPath获取它:

Getting it by XPath:

checkbox = driver.find_element_by_xpath('//*[@id="agreed"]')

两个都给我

Message: element not visible

我也尝试过等待它可见,但它只是等待并最终为我提供以下内容:

I've also tried waiting for it to be visible but it just waits and eventually gives me the following:

checkbox = WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="agreed"]')))
checkbox.click()

输出:

Message: 

推荐答案

要单击与元素关联的复选框,其文本为我同意API服务协议无需调用visibility_of_element_located(),您需要调用 element_to_be_clickable() ,然后可以使用以下任一解决方案:

To click on the checkbox associated with the element with text as I agree with the API Servive Agreement instead of invoking visibility_of_element_located() you need to invoke element_to_be_clickable() and you can use either of the following solutions:

  • CSS_SELECTOR:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='agreed']"))).click()

  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='agreed']"))).click()
    

  • 这篇关于消息:尝试通过Selenium和Python单击复选框时,元素不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆