Returning null element when using following and preceding XPath selector

0 votes

I am working on a project. In that, I am trying to find the preceding element using selenium webdriver with the use of xpath selector. I’m selecting a label using xpath but it returns the null value using below code line

driver.FindElement(By.XPath("//*[@id='myDvId']/label/preceding-sibling::input[1])"));

 

Below is the HTML:

<div id="myDvId">

    <input type="checkbox" id="chxInputId"></input> <!-- THIS IS INPUT CHECKBOX WITH NO ID or CLASS NAME -->

    <label for="chxInputId">My Label<label>

</div>

Please help me in this

Jun 8, 2018 in Selenium by Martin
• 4,220 points
83 views

1 answer to this question.

0 votes

To identify the <input> tag with respect to the <label> tag with text as My Label you can use the following line of code :

driver.FindElement(By.XPath("//div[@id='myDvId']/label[contains(.,'My Label')]//preceding::input[1])"));
answered Jun 8, 2018 by Samarpit
• 5,130 points

Related Questions In Selenium

0 votes
1 answer

Locating an element by value using css selector using Selenium WebDriver

Below code will help By.cssSelector("td[class='someclass'][value='u ...READ MORE

answered Apr 9, 2018 in Selenium by coldcode
• 2,020 points
100 views
0 votes
1 answer
0 votes
1 answer

Using XPath preceding-sibling correctly

Since all buttons are on the same ...READ MORE

answered Apr 18, 2018 in Selenium by Vardy
• 2,250 points
3,618 views
0 votes
1 answer

Selecting xpath for with multiple conditions using Selenium and Python

Use the XPath in either of the following ways ...READ MORE

answered Jul 12, 2018 in Selenium by Samarpit
• 5,130 points
4,179 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,070 points
441 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
789 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
432 views
0 votes
1 answer

How to get next sibling element using XPath and Selenium for Java?

Below code will help you: Try following-sibling axis : WebElement ...READ MORE

answered May 15, 2018 in Selenium by Samarpit
• 5,130 points
5,147 views
0 votes
2 answers