Selecting checkbox Is not big task In selenium webdriver If check box has ID, Name or any other proper locator. You can check It very easily using .Click() method. .Click() Is generic method and you can use It to click on any element like select radio button, check the check box or clicking on
any other element. But supposing you have a list of Items with checkbox and any checkbox do not have any Identifier then how will you select specific checkbox? NEXT POST will describe how to select checkbox from table using following-sibling and following-sibling
Solution 1 : Using Absolute XPath
Consider checkbox list given on THIS PAGE. All the checkbox have type attribute with same value checkbox. Any of them do not have any proper locator using which I can locate specific checkbox. I wants to select check box which Is located In "Cow" row. One way Is using absolute XPath as bellow.
//div[@id='post-body-536524247070242612']/div[1]/table/tbody/tr[3]/td[1]/input
At place of using absolute XPath, We can use functions like last() and position() In XPath to locate specific checkbox.
Read more webdriver tips and tricks on THIS PAGE.
Read more webdriver tips and tricks on THIS PAGE.
Solution 2 : Using position() Function In XPath
In bellow given xpath, [@type='checkbox'] will locate checkbox and function [position()=3] will locate checkbox which Is located on 3rd position from top. You can change your position number as per requirement.
xpath=(//input[@type='checkbox'])[position()=3]
Solution 3 : Using last() Function In XPath
In bellow given xpath, [last()-1] function will locate 2nd last checkbox which Is for Lion.
xpath=(//input[@type='checkbox'])[last()-1]
To locate last checkbox, You can use It as bellow.
xpath=(//input[@type='checkbox'])[last()]
Here Is the practical webdriver example to select checkbox using position() and last() functions.
package Testing_Pack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Checkboxpos {
WebDriver driver;
@BeforeTest
public void setup() throws Exception {
driver =new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://only-testing-blog.blogspot.com/2014/09/temp.html");
}
@Test
public void selectCheck(){
//To select Cow checkbox using position() function.
driver.findElement(By.xpath("(//input[@type='checkbox'])[position()=3]")).click();
//To select Lion checkbox using last() function.
driver.findElement(By.xpath("(//input[@type='checkbox'])[last()-1]")).click();
//To select Tiger checkbox using last() function.
driver.findElement(By.xpath("(//input[@type='checkbox'])[last()]")).click();
}
}
You can use same thing for any element to locate It from list of same Items. You can learn how to get XPath or CSS path of any element using firebug and firepath as described in THIS POST.
thanks for this trick. There is also another shortcut for not using position
ReplyDelete(//*[@type='checkbox'])[2] = (//*[@type='checkbox'])[position()=2]
we can directly use the index instead of using position()
Also note it starts from 1 and not 0. regards
Hi Gaurav, I can not find type = 'checkbox' in XML file. Is it a keyword that we can apply to every checkbox ?
DeleteHi Kieu , goto http: // only-testing-blog.blogspot.in/2014/09/temp.html and right click on the checkbox in front of cat and click inspect.. You will see that < input type="checkbox" >
DeleteIts not a keyword its just an attribute by which we are trying to get data. Just for reference
thanks... i came across lots of articles, finally my issue got solved after going thru your blog ..:)
ReplyDelete