【问题标题】:How to select following element in selenium?如何选择硒中的以下元素?
【发布时间】:2019-04-27 20:36:28
【问题描述】:

我有一个 div,其中包含一个输入,其中包含一个标识符作为 id 和一个标签。使用我的 selenium 方法,我可以轻松地将输入选择为 webElement,但是如何通过 Input WebElement 获取标签 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>

这是 selenium 中选择 webElement 的方法,目前只选择输入而不是标签。

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

问题是只有标签是可点击的,因此我需要从输入字段中选择标签,我该怎么做?

【问题讨论】:

  • 你能分享你当前的代码吗?您应该以minimal, complete, verifiable example 的形式提出您的问题
  • 我已经添加了我的 selenium 选择器代码
  • @Simon.B 为什么需要从输入字段中选择标签?你的用例是什么?访问&lt;input&gt;&lt;label&gt;?

标签: c# selenium selenium-webdriver xpath


【解决方案1】:

,您不会在&lt;label&gt; 节点上调用click(),而是在&lt;input&gt; 节点上调用。

根据 HTML 调用 &lt;input&gt; 标签上的 click() 相对于 &lt;label&gt; 标签,您可以使用以下解决方案:

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();
    }

【讨论】:

    【解决方案2】:

    试试这个:

    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();
            }
    

    或者这个:

    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();
                }
    

    后一个 - 我不太擅长 C#,所以也许你必须把 {questionid} 放在那里,而不仅仅是 questionid

    【讨论】:

      【解决方案3】:

      有一些选择:

      1. 选择带有 questionId 的元素后,您可以继续进一步选择标签。 例如:
      IWebElement RateQuestionElement = this.Driver.FindElement(By.Id(questionid)); 
      
      IWebElement RateQuestionLabel = RateQuestionElement.FindElement(By.Css("label"));
      
      
      
      RateQuestionLabel.click();
      

      1. 您可以使用 CssSelector 例如:
      myCssSelector = "#myQuestionId label"
      
      IWebElement RateQuestionElement = this.Driver.FindElement(By.Css(myCssSelector));
      
      RateQuestionElement.click();
      

      我没有尝试自己执行它,但概念就在那里

      【讨论】:

        猜你喜欢
        • 2021-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-09
        • 1970-01-01
        • 2018-12-05
        相关资源
        最近更新 更多