Selecting Checkbox From Table Based On Preceding Or Following Sibling Column Value In WebDriver

Locating element's past times Its ain references Is slow but onetime you lot require to located them based on reference of other elements. Earlier nosotros accept acquire how to locate checkbox based on Its seat In THIS POST. Now I accept i tabular array together with kickoff column of tabular array Is checkbox. s column Is value(some
text) together with based on that next sibling column's value(text), I wants to select cheque box from 1st column. I accept this scenario In i of my electrical flow project. Same way, I accept to choose checkbox based on preceding sibling column's value(text). See Image bellow.


Here cheque box create non accept Its ain Identifier bet nosotros accept to Identify It based on related previous or adjacent cell's text value. In this case, We tin role sibling concept In XPath to locate preceding or next sibling element.

following-sibling
To choose Dog checkbox, We accept to role following-sibling In xpath equally checkbox jail mobile telephone Is next to "Dog" text jail mobile telephone In table. So XPath to locate Dog checkbox Is equally bellow.
//td[contains(text(),'Dog')]/following-sibling::td/input[@type='checkbox']

Here, 
  • //td[contains(text(),'Dog')] volition locate "Dog" text cell.
  • /following-sibling volition locate all siblings later the electrical flow node.
  • td/input[@type='checkbox'] volition locate the checkbox

preceding-sibling
Here Is contrary condition. Checkbox comes kickoff together with value text comes concluding together with thence concept Is same but nosotros accept to role give-and-take preceding-sibling In XPath equally bellow.
//td[contains(text(),'Cow')]/preceding-sibling::td/input[@type='checkbox']

Here, 
  • //td[contains(text(),'Cow')] volition locate "Cow" text cell.
  • /preceding-sibling will locate all siblings earlier the electrical flow node.
  • td/input[@type='checkbox'] volition locate the checkbox
So directly If you lot wants to write exam to choose higher upwards both textbox, It volition move equally bellow.
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;  world degree Slibings {  WebDriver driver;  @BeforeTest  world void setup() throws Exception {   driver =new FirefoxDriver();        driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);   driver.get(" ");  }    @Test  world void selectCheck(){   //Locating chemical cistron using preceding-sibling In XPath.   driver.findElement(By.xpath("//td[contains(text(),'Cow')]/preceding-sibling::td/input[@type='checkbox']")).click();   //Locating chemical cistron using following-sibling In XPath.   driver.findElement(By.xpath("//td[contains(text(),'Dog')]/following-sibling::td/input[@type='checkbox']")).click();    } }

You tin role this sibling concept anywhere to locate whatever chemical cistron similar checkbox, radiobutton, textbox etc. If It Is subject on other value.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM

http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post