简体   繁体   中英

How can I click on this element using XPATH in WebDriver with Java?

Here is the HTML:

<li>
<input type="checkbox" checked="" name="selectedMstrPrivGroupList[9].mstrAuthorities[0].status"/>
Add Dexter
</li>

How could this element be clicked in WebDriver? It is a check box. And I want to use XPath as I have close to 30+ check boxes in the page. So that I can create a generic method and pass only the WebElement. I tried the following but didn't work.

Driver.findElement(By.xpath("//input[contains(.,'Add Dexter')]")).click();

如果您要在页面上单击“添加Dexter”旁边的复选框,则可以使用:

Driver.findElement(By.xpath("//li[contains(.,'Add Dexter')]//input[@type='checkbox']")).click();

这是什么:

  Driver.findElement(By.xpath("//input[@name='selectedMstrPrivGroupList[9].mstrAuthorities[0].status']")).click();

你可以这样使用, driver.findElement(By.xpath("//li[contains(text(),'Add Dexter')]")).click()

You can use xpath to click on the element as below:

driver.findElement(By.xpath("//input[text()='Add Dexter']")).click();

You can also click on that element by using cssSelector instead of xpath as below:

driver.findElement(By.cssSelector("input:contains(^Add Dexter$)")).click();

Note: CssPath/CssSelector is faster than xpath. So it's better to use cssSelector than xpath in most cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM