Locating element's by Its own references Is easy but sometime you need to located them based on reference of other elements. Earlier we have learn how to locate checkbox based on Its position In THIS POST. Now I have one table and first column of table Is checkbox. 2nd column Is value(some
text) and based on that following sibling column's value(text), I wants to select check box from 1st column. I have this scenario In one of my current project. Same way, I have to select checkbox based on preceding sibling column's value(text). See Image bellow.
Here check box do not have Its own Identifier bet we have to Identify It based on related previous or next cell's text value. In this case, We can use sibling concept In XPath to locate preceding or following sibling element.
following-sibling
To select Dog checkbox, We have to use following-sibling In xpath as checkbox cell Is following to "Dog" text cell In table. So XPath to locate Dog checkbox Is as bellow.
//td[contains(text(),'Dog')]/following-sibling::td/input[@type='checkbox']
Here,
- //td[contains(text(),'Dog')] will locate "Dog" text cell.
- /following-sibling will locate all siblings after the current node.
- td/input[@type='checkbox'] will locate the checkbox
preceding-sibling
Here Is reverse condition. Checkbox comes first and value text comes last so concept Is same but we have to use word preceding-sibling In XPath as bellow.
//td[contains(text(),'Cow')]/preceding-sibling::td/input[@type='checkbox']
Here,
- //td[contains(text(),'Cow')] will locate "Cow" text cell.
- /preceding-sibling will locate all siblings before the current node.
- td/input[@type='checkbox'] will locate the checkbox
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 Slibings {
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/2015/01/table-with-checkbox.html");
}
@Test
public void selectCheck(){
//Locating element using preceding-sibling In XPath.
driver.findElement(By.xpath("//td[contains(text(),'Cow')]/preceding-sibling::td/input[@type='checkbox']")).click();
//Locating element using following-sibling In XPath.
driver.findElement(By.xpath("//td[contains(text(),'Dog')]/following-sibling::td/input[@type='checkbox']")).click();
}
}
You can use this sibling concept anywhere to locate any element like checkbox, radiobutton, textbox etc. If It Is dependent on other value.
Hi,
ReplyDeleteWebDriver driver = new FirefoxDriver();
driver.get("http://only-testing-blog.blogspot.in/2015/01/table-with-checkbox.html");
WebElement dTable= driver.findElement(By.xpath("//*[id='post-body-3107268830657760720']/div[1]/table/tbody"));
Not able to locate this table. Please help...
Hi You have missed @ symbol before id . It should be @id
DeleteJust to highlight we have to use sibling to refer to the next immediate , without mentioning sibling system considers all the following elements instead of the immediate one
DeleteHi,
ReplyDeleteI want xpath for the checkbox before one hyperlink QUA1:ACC26419,I want xpath with preceding sibling QUA1:ACC26419, please help me above procedure isn't working for me. WHERE
XPATHS
form xpath = //form[@name='TPADMINFORM']/table[2]
checkbox xpath = //form[@name='TPADMINFORM']//tr[2]//input[@name='SETUPAFITEMNO']
hyperlink which is after checkbox xpath = //a[.='QUA1:ACC26419'].
HELP ME IN FINDING XPATH BASED ON HYPER LINK WHICH IS AFTER THIS CHECKBOX
Hi,
ReplyDeleteCan I know how to select check box based on two preceding siblings