Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
<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?

share|improve this question
    
If you tried By.id | By.xpath with findElement and neither of them work than the selected id may be generated. –  Gyorgy.Hegedus yesterday
    
Sounds like the ID is dynamically generated. –  Arran yesterday
    
Since the ID looks like it's dynamically generated you need to look for some distinct ids, classes or elements, such that you can find from them the div with the input. This can then be done for example with positional selector <some specific selector higher up>/div[1]/input[1]. –  Artjom B. yesterday
add comment

3 Answers

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.

share|improve this answer
add comment

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();
share|improve this answer
add comment

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();
share|improve this answer
    
the ID value change everytime, so i cant use find ID. i need another way to do this –  user29542 yesterday
    
thank you loknath, thats what i did too, using 'contains(@title, 'Select this email'); i cant vote since i am new here –  user29542 2 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.