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,320 points
2,932 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,910 points

Related Questions In Selenium

0 votes
1 answer

Which exception is raised when an element is not found in an HTML DOM using XPath

Hey, A stale element reference exception is thrown in one of two cases If ...READ MORE

answered Jul 30, 2020 in Selenium by Sirajul
• 59,230 points
10,316 views
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=' ...READ MORE

answered Apr 9, 2018 in Selenium by coldcode
• 2,080 points
1,710 views
0 votes
1 answer
0 votes
2 answers

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,500 points
12,715 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,610 views
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,910 points
6,684 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,553 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,910 points
20,325 views
0 votes
5 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP