Jose
Jose

Reputation: 310

xpath: unable to click on any checkbox

Unable to click on checkbox by xpath. I want to make this process dynamic in order to pass any parameter and then click where i need. However, i have tried with get input finding the text but something should be wrong because when i look for the parent of text (input) it goes to another property. How can i solve this?

HTML Code

<ul class="collapsibleList" style="display: block;">
<li class=""> 
   <input type="checkbox" id="000375" name="000370" class="">
   <div style="display: inline" id="000375_lit" class="">2.5.1 Formalizar</div>
</li>

HTML

enter image description here

xpath try

//*[text()='2.5.1 Formalizar']/ancestor::li[contains(@class,'collapsibleList')]/ul/li/input

Upvotes: 0

Views: 108

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

To click on any of the Check Box with respect to the chosen text e.g. 2.5.1 Formalizar you can use the following solution:

  • You can create a function which will take inputs as the text to locate the relative <input> tag and click as follows :

    public void clickItem(String itemName)
    {
        driver.findElement(By.xpath("//ul[@class='collapsibleList']/li//div[.='" + itemName + "']//preceding::input[1]")).click();
    }
    
  • Now you can call the function clickItem() with any string argument as follows:

    clickItem("2.5.1 Formalizar");
    

Upvotes: 1

Murthi
Murthi

Reputation: 5347

Instead of click of the checkbox, you can click on the label next to it. It may work.

//ul/li/span[text()='2.5.1 Formalizar']

In java,

driver.findElement(By.xpath("//ui/li/span[text()='2.5.1 Formalizar']").click();

Upvotes: 0

Related Questions