How to select following element in selenium?

following-sibling xpath selenium
selenium get next sibling element python
selenium get parent element python
xpath next element
xpath using class
selenium find next element
descendant xpath
xpath tutorial

I have a div which contains a input with an identifier as id and a label in it. with my selenium method i can easily select the input as a webElement but how can i get the label webElement via the Input WebElement?

   <div class="item-selectable small-2 radio-item columns xsmall-12 item-selectable-stacked">
    <input id="0-3" name="RatingQuestions[0].Score" type="radio" value="3" checked="">
 <label for="0-3">
</label>
 </div>

This is the method in selenium to select the webElement, whcih currently selects only the input and not like it should the label.

public void RateQuestion(int questionnumber, int rating)
        {
            string questionid = questionnumber.ToString() + "-" + rating.ToString();
            IWebElement RateQuestionElement = this.Driver.FindElement(By.Id(questionid));
            RateQuestionElement.Click();
        }

The issue is that only the label is clickable and therefore i need to select the label from the input field, how can i do this?

No, you don't invoke click() on <label> nodes but on <input> nodes.

As per the HTML to invoke click() on the <input> tag with respect to the <label> tag you can use the following solution:

public void RateQuestion(int questionnumber, int rating)
    {
        string questionid = questionnumber.ToString() + "-" + rating.ToString();
        IWebElement RateQuestionElement = this.Driver.FindElement(By.XPath("//label[@for='" + questionid + "']"));
        RateQuestionElement.Click();
    }

XPath Contains, Sibling, Ancestor, AND OR, Parent, Start with, Axes , By using 'contains' function in XPath, we can extract all the elements which graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4" whose text is 'SELENIUM' then at last we will find all of the sibling elements of How to Select Value from DropDown using Selenium Webdriver. Select class in Selenium WebDriver: How to select an option from the drop-down menu? I will help you guys understand how this Select method works with a real-time example. In this case, I will consider working on a famous e-commerce website facebook.com.

Try this:

public void RateQuestion(int questionnumber, int rating)
        {
            string questionid = questionnumber.ToString() + "-" + rating.ToString();
            IWebElement RateQuestionElement = this.Driver.FindElement(By.Id(questionid)).FindElement(By.Xpath("./../../label"));
            RateQuestionElement.Click();
        }

Or this:

public void RateQuestion(int questionnumber, int rating)
            {
                string questionid = questionnumber.ToString() + "-" + rating.ToString();
                IWebElement RateQuestionElement = this.FindElement(By.Xpath("//*[contains(local-name(), 'label') and contains(@for, questionid)]"));
                RateQuestionElement.Click();
            }

The latter one - I'm not that good with C#, so perhaps you'll have to put {questionid} there, rather than just questionid.

Selenium webdriver select element "next to" already selected , I would try to avoid a find strategy that relies upon the text content as typically this most likely to change over time. It would also prevent you from finding an  Note: if you include the Select declaration and cast in your class, just below the private WebElement declaration, you will get runtime errors when the class is instantiated; to avoid this, you can put the Select declaration and class in the method which does the interaction with the screen control.

There are some options:

  1. After you select the element with questionId, the you can continue further to select the label. e.g:
IWebElement RateQuestionElement = this.Driver.FindElement(By.Id(questionid)); 

IWebElement RateQuestionLabel = RateQuestionElement.FindElement(By.Css("label"));



RateQuestionLabel.click();

or

  1. You can use CssSelector e.g:
myCssSelector = "#myQuestionId label"

IWebElement RateQuestionElement = this.Driver.FindElement(By.Css(myCssSelector));

RateQuestionElement.click();

I have not tried to execute it myself, but the concept is there

XPath tutorial for Selenium, And '*' is used for selecting all the element nodes descending from the current node with following-sibling, Selects all siblings after the current node. There are two more alternatives to select option value from drop down or list box in selenium webdriver software testing tool. 1. Selecting Option By Value and 2. Selecting Option By Index. You can use any one from these three to select value from list box of your software web

What is following-sibling in selenium?, And so it is not possible to navigate to the 4rd li. Here we can make use of the following-sibling to identify our element by. //li[@class='  In this tutorial, we will learn how to handle Drop Down and Multiple Select Operations. Select Option from Drop-Down Box. Before we can control drop-down boxes, we must do following two things: Import the package org.openqa.selenium.support.ui.Select; Instantiate the drop-down box as a "Select" object in WebDriver

How to get next sibling element using XPath and Selenium for Java , Getting a parent element when using Selenium for Java and XPath is something like this findElement(By.xpath("following-sibling::*"));. Note: Select class only works for elements with <select> tags. Now, once you got the oSelect object which is a SELECT object, you can access all the methods resides in side the SELECT class by typing oSelect + dot. Different Select Commands. In this chapter, we will learn how to deal with DropDown and Multi-Select elements.

Using Contains, Sibling, Ancestor to Find Element in Selenium , Therefore, we are using 'following-sibling' function that has class as 'c' and thereby selecting the hyperlink with a tag as 'a'. This particular web  Getting a parent element when using Selenium for Java and XPath is something like this. WebElement parent = child.findElement(By.xpath("..")); But how do I get the next sibling of an element?

Comments
  • Can you share your current code please? You should ask your question as minimal, complete, verifiable example
  • I've added my selenium selector code
  • @Simon.B Why do you need to select the label from the input field? What is your usecase? Access the <input> or the <label>?