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.

I'm trying to figure out the answer to a problem I've recently discovered. I have some HTML as indicated below and what I want to do, is send an HTML snipped to selenium so it can click on the checkbox. However, I don't want to lookup by id because as you can see, the id is ugly. Instead, I am looking for an alternative method.

HTML:

<li class= "zone odd open night">...</li>
<li class= "zone even open night">...</li>
<li class= "zone odd open night">...</li>
  <label for="srr-2-1397538000">Room 226 1:00 AM</label>
  <input type="checkbox" name="srr-2-1397538000" id="srr-2-1397538000" value="Y" class="interactive">
  <span class-"drag-handle">...</span>
</li>
<li class="zone even open night">...</li>

As you can see, I'm trying to lookup that specific checkbox to click but I don't want to do it by looking up via id. I would rather look up by "Room" or something more generic.

Does anyone have any suggestions as to how to do this? With using selenium, or some other webdriver?

Also, I've tried the classic:

element = driver.find_element_by_id("srr-2-1397538000")
element.click()

But as I said, this is not what I want.

Thank You

share|improve this question

2 Answers 2

Can you use the tag name?

element = driver.find_element_by_tag_name('input')
element.click()
share|improve this answer

If you want to look it up by Room, you can use xpath:

element = driver.find_element_by_xpath("//label[contains(text(),'Room')]/following-sibling::input")
element.click()
share|improve this answer

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.