1

While trying to automate my application, one of the web-page has a toggle button inside a table and I need to select the same. Below is the image of HTML code.I am unable to copy paste the code hence attaching the image.

enter image description here

Below is the code tried but getting error as unable to locate element.

IWebElement btn3 = driver.FindElement(By.XPath("//input[@id='livingExpenseConsent']/ancestor::span"));
Actions action2 = new Actions(driver);
action2.MoveToElement(btn3).Click().Perform();

Kindly suggest the right way to locate the element.

2
  • It would be better if you provide html code instead of an image. BTW the span with the slider is not an ancestor. May be is a following-sibling but I don't know if Selenium supports that. Commented Jul 6, 2020 at 18:13
  • Could you not simply set your xpath to click on the input element? Commented Jul 6, 2020 at 19:37

2 Answers 2

2

ancestor is generally used to search for parent elements or greater. If it is the span element that you are wanting to set your XPath as then using following-sibling should work. This looks at all the elements on the same level as the element being used to search for following-sibling's. In your case the input element.

XPath: //input[@id='livingExpenseConsent']/following-sibling::span

Sign up to request clarification or add additional context in comments.

Comments

2

Try with the below xpath.

//input[@id='livingExpenseConsent']/following-sibling::span

1 Comment

Please accept the answer for the benefit of future readers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.