Search code examples
watirwatir-webdriver

Unable to check/uncheck a checkbox


I am trying to check different checkbox available but I am not able to get the id for the same.I tried xpath :

@browser.checkbox(:xpath => "//SPAN[@id='incentive_4000215652']").set and @browser.checkbox.span(:id => 'incentive_4000215652').set

The link to page is https://secure.bestprice.rankingsandreviews.com/nc/configurator/301937

Click on Edit besides Exterior color and click Incentives tab.

Any help would be appreciated.Thanks!


Solution

  • Use this

    browser.element(text: "2017 GM Conquest Cash").parent.preceding_sibling.span.click
    

    Or use this

    browser.element(xpath: "//*[normalize-space()='2017 GM Conquest Cash']/../preceding-sibling::td/span").click
    

    If you change the text inside, then it click the corresponding checkbox.

    Second one is bit faster than the first one, because WATIR doesn't go to form the xpath since xpath is directly available to you.

    Full code follows here

    browser.goto('https://secure.bestprice.rankingsandreviews.com/nc/configurator/307715')
    
    browser.element(:id => 's2id_select_style').click
    
    browser.element(xpath: ".//*[@id='select2-drop']/div/input").send_keys('Convertible LT 1LT', :tab)
    
    browser.element(id: 'edit_color').click
    
    browser.span(text: 'Incentives').click
    
    browser.element(text: "2017 GM Conquest Cash").parent.preceding_sibling.span.click
    

    Or

    browser.element(xpath: "//*[normalize-space()='2017 GM Conquest Cash']/../preceding-sibling::td/span").click