简体   繁体   English

如何使用硒单击此复选框

[英]How do I click on this check box using selenium

I would like to select this checkbox using selenium我想使用 selenium 选择此复选框

<input type="checkbox" name="rg" onchange="onCheckReg(this)" id="beauty_perfume_screening__c" value="beauty_perfume_screening__c" tabindex="-1" aria-labelledby="check-button-Beauty Perfume Screening check-group-header">

I tried finding by xpath an by id but always get the error unable to locate element我尝试通过 xpath 和 by id 查找,但总是出现无法定位元素的错误

wd.find_element_by_id("beauty_perfume_screening__c").click()

Any Ideas?有任何想法吗?

Use xpath使用 xpath

driver.find_element_by_xpath("//input[@name='rg']").click()

and recommended is to use one of the wait methods -并建议使用其中一种等待方法 -

WebDriverWait(driver, 10).until(
    EC.presence_of_element_located(By.XPATH, "//input[@name='rg']")

For selecting iframe -对于选择 iframe -

root1 = driver.find_element_by_xpath("//iframe")
driver.switch_to.frame(root1)

在此处输入图片说明

To click on the element with text as save you can use either of the following Locator Strategies :要单击带有文本的元素作为保存,您可以使用以下任一定位器策略

  • Using id :使用id

     wd.find_element_by_id("beauty_perfume_screening__c").click()
  • Using css_selector :使用css_selector

     wd.find_element_by_css_selector("input#beauty_perfume_screening__c[name='rg'][value='beauty_perfume_screening__c']").click()
  • Using xpath :使用xpath

     wd.find_element_by_xpath("//input[@id='beauty_perfume_screening__c' and @name='rg'][@value='beauty_perfume_screening__c']").click()

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :理想情况下,要单击元素,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Using ID :使用ID

     WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.ID, "beauty_perfume_screening__c"))).click()
  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#beauty_perfume_screening__c[name='rg'][value='beauty_perfume_screening__c']"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='beauty_perfume_screening__c' and @name='rg'][@value='beauty_perfume_screening__c']"))).click()
  • Note : You have to add the following imports :注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

Important : Incase the element is within an <iframe> you have to switch to the <iframe> first.重要提示:如果元素在<iframe>您必须先切换到<iframe>

You can find a couple of relevant discussions in:您可以在以下位置找到一些相关讨论:


References参考

You can find a couple of relevant discussions on NoSuchElementException in:您可以在以下位置找到有关NoSuchElementException的一些相关讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM