简体   繁体   中英

How to click a checkbox using selenium

<div id="yui_3_16_0_1_1399697074576_1339" class="cbox " role="gridcell">
<input id="yui_3_16_0_1_1399697074576_1338" type="checkbox" tabindex="-1" 89513626107905="" aria-label="Message " title="Select this email">
<span id="yui_3_16_0_1_1399697074576_1340" class="icon"></span>
</div>`

The above HTML is from firebug.

I want to click the checkbox, its ID is id="yui_3_16_0_1_1399697074576_1338" from above. I tried using by.id and by.path , however neither of them work. The following is what I tried:

By.id("yui_3_16_0_1_1399697074576_1338")
By.xpath("//input[@id='yui_3_16_0_1_1399697074576_1339'

Can someone help me on this?

if the id is static :

 string checkboxXPath = "//input[contains(@id, 'yui_3_16_0_1_1399697074576_1338')]"

for Dynamic Tags value:

           string checkboxXPath = "//input[contains(@type,'checkbox') and                       
          contains(@title,'Select this email')]"

IWebElement elementToClick = driver.FindElement(By.XPath(checkboxXPath));
elementToClick.Click();

You NEVER want to match on an ID like that. What I'd recommend, is matching on something a little more "unique".

Try:

By.cssSelector("input[type='checkbox'][title='Select this email']")

This selector above will match your <input/> perfectly. And, it's not coupled with the Yahoo UI so if you or your developers ever change software, it will account for that.

You have to get the element using the id and make a click. It is similar to button click

driver.findElement(By.id("yui_3_16_0_1_1399697074576_1338")).click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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