How to select checkbox using position or last function in Xpath
Posted By : Anuj Gupta | 04-Sep-2018
Today, we will discuss about selecting a checkbox using position() or last() functions. Well, selecting a checkbox is not a difficult task if a checkbox has an id, name, and class or a combination of these attributes.
If these attributes are not present then we will not be able to locate checkbox or radio buttons. So, let's discuss in a brief:
1) Using Position() function in
If you want to select a checkbox on the basis of its position using
Let's say, if you want to select a second and fourth position then we can use below mentioned command:
xpath=(//input[@type='checkbox']) [position()=2]; xpath=(//input[@type='checkbox']) [position()=4];
2) Using Last() function in
You can use last() function to select the last checkbox among the similar objects.
xpath=(//input[@type='checkbox'])[last()]
If you want to select a second last checkbox then you can also use the below mentioned command:
xpath=(//input[@type='checkbox'])[last()-1]
So, here is a small example to make it clearer.
package Selenium; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TestNG { public WebDriver driver; @BeforeTest public void setup() throws Exception { System.setProperty("webdriver.chrome.driver", "/home/anuj/chromedriver"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get("http://only-testing-blog.blogspot.in/2014/09/temp.html"); } @Test public void selectCheck(){ //To select C++ checkbox using position() function. driver.findElement(By.xpath("(//input[@type='checkbox'])[position()=2]")).click(); //To select PHP checkbox using last() function. driver.findElement(By.xpath("(//input[@type='checkbox'])[last()-1]")).click(); //To select Ruby checkbox using last() function. driver.findElement(By.xpath("(//input[@type='checkbox'])[last()]")).click(); } }
The first image shows the five checkbox fields and on the basis above program, C++, PHP and Ruby checkbox got selected which will show as an output in the second image.
You can also use the same thing for any element to locate it from the list of similar items.
If you enjoyed this blog post, share it with a friend!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Anuj Gupta
Anuj is a passionate QA engineer and an optimistic person. He is fond of playing Chess and Basketball. He always comes up with the new ideas and is a good team player.