【问题标题】:selenium checkbox click not working in python硒复选框单击在python中不起作用
【发布时间】:2019-10-13 01:58:49
【问题描述】:

https://agent.loadbook.in/#/login 当我进入注册表单时,表单有一个复选框,因为我同意该条件。这是一个输入标签复选框。当我按 id 选择标签时,当我执行 click 或 send_key 提交其显示复选框不可点击时。

以下所有方法都不起作用

driver.find_element_by_xpath('//div[@class="switch__container"]//input').click()

driver.find_element_by_xpath('//div[@class="switch__container"]//input').send_keys(Keys.ENTER)

driver.find_element_by_xpath('//div[@class="switch__container"]//input').submit()

driver.find_element_by_xpath('//div[@class="switch__container"]//input').send_keys("after")

driver.find_element_by_xpath('//div[@class="switch__container"]//label').click()



driver = webdriver.Chrome(options=chrome_options)
driver.get("https://agent.loadbook.in/#/login")


driver.find_element_by_partial_link_text("Create an account").click()

尝试:

driver.find_element_by_xpath('//div[@class="switch__container"]//input').click()
# driver.find_element("name","username").send_keys("test")
# driver.find_element("name","email").send_keys("test@test.com")
# driver.find_element("name","phone").send_keys("99999999")
# driver.find_element("name","password").send_keys("12345")

除了 NoSuchElementException 作为异常: print("未找到")

selenium.common.exceptions.ElementNotVisibleException:消息: 元素不可交互

【问题讨论】:

  • 在注册页面上它不是复选框实际上是它的切换

标签: python selenium checkbox css-selectors webdriverwait


【解决方案1】:

您需要使用 Selenium 的 Explicit Wait 功能等待元素出现在 DOM 中。

  1. 使用WebDriver.get()函数打开页面

    driver.get("https://agent.loadbook.in/#/login")
    
  2. Click“创建帐户”链接:

    driver.find_element_by_link_text("Create an account").click()
    
  3. Wait 直到复选框加载并变为可点击:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "switch_div")))
    
  4. 点击复选框:

     driver.find_element_by_class_name("switch_div").click()
    

更多信息:How to use Selenium to test web applications using AJAX technology

【讨论】:

    【解决方案2】:

    click() 在与文本相关的复选框上显示为我同意条款和条件以及隐私政策,您必须为 元素诱导 WebDriverWait可点击,您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      # options.add_argument('disable-infobars')
      driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      driver.get("https://agent.loadbook.in/#/login")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Create an account"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='switch-shadow']"))).click()
      
    • 浏览器快照:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多