user3209294
user3209294

Reputation: 1

To click on the check box of all [Ruby Selenium-webdriver]

Button as shown below is lined in large quantities. Button or decrease it or increase depending on the day. contents of "value" only changes.

<input type="checkbox" value="021258" name="xbutton"></input>
<input type="checkbox" value="084192" name="xbutton"></input>
<input type="checkbox" value="095842" name="xbutton"></input>
<input type="checkbox" value="096325" name="xbutton"></input>
...
...

I would like to operate like to check all of this. I think so that it can respond to various situations, and to non-xpath did. It does not seem to click only one of the first that it is a thing of the following.

driver.find_element(:name, "xbutton").click

Upvotes: 0

Views: 3208

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

I first saved, just for testing all the html part, in a file test.html as below :

<input type="checkbox" value="021258" name="xbutton"></input>
<input type="checkbox" value="084192" name="xbutton"></input>
<input type="checkbox" value="095842" name="xbutton"></input>
<input type="checkbox" value="096325" name="xbutton"></input>

Now to check each of those check boxes, I wrote the code as below :

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "file:///home/kirti/workspace/Ruby/test.html"

elements_to_click = driver.find_elements(:xpath, "//input[@name='xbutton']")
# below code will check only those check boxes which are not checked previously.
elements_to_click.each { |e| e.click if e.attribute('checked').nil? }

Below is the screenshot :

**check box** :

Upvotes: 1

Related Questions