Xpath 无法点击按钮(按钮 ID 和值动态变化)Selenium WebDriver Java

Xpath can't click to button (Button id & value dynamically changes) Selenium WebDriver Java

<form action="/assessments/1290/questions" id="questionform" method="post">
  <div class="happiness-reaction__item animated fadeIn selected">
    I often get upset over minor issues.
    <div class="inputs">

      <input id="choice_14832" value="14832" name="Answers[0].Choices" data-scv-reaction-id="strongly-disagree1" spellcheck="true" type="radio">
      <label class="u" for="choice_14832">
        <i class="sc-strongly-disagree"></i>
      </label>

      <input id="choice_14833" value="14833" name="Answers[0].Choices" data-scv-reaction-id="slightly-disagree1" spellcheck="true" type="radio">
      <label class="u" for="choice_14833">
        <i class="sc-slightly-disagree"></i>
      </label>

      <input id="choice_14834" value="14834" name="Answers[0].Choices" data-scv-reaction-id="none1" spellcheck="true" type="radio">
      <label class="u" for="choice_14834">
        <i class="sc-none"></i>
      </label>

      <input id="choice_14835" value="14835" name="Answers[0].Choices" data-scv-reaction-id="slightly-agree1" spellcheck="true" type="radio">
      <label class="u" for="choice_14835">
        <i class="sc-slightly-agree"></i>
      </label>

      <input id="choice_14836" value="14836" name="Answers[0].Choices" data-scv-reaction-id="strongly-agree1" spellcheck="true" type="radio">
      <label class="u" for="choice_14836">
        <i class="sc-strongly-agree"></i>
      </label>
    </div>
  </div>
                 .....

我想点击那个单选按钮。由于 ID 和值等动态变化,我应该能够通过使用 XPATH 进行简单的点击,但我不能。我没有收到任何错误,但按钮一直未被点击。

Check this picture please

到目前为止我无法点击:

WebElement button = driver.findElement(By.xpath(".//*[@id='questionform']/div[1]/div/label[1]/i"));
button.click();

已编辑:我添加了此信息,以防万一有人想要有关我正在谈论的页面的更多详细信息。必须有一种方法可以在没有解决方法的情况下单击那个简单的按钮。

"I know that would be too much effort to ask , but you can go "supercareer.com" , 并根据个性进行评估, 看看我实际上在说什么。注意: 不必是真实的邮件, 易于注册 1 点击, 无需验证你可以访问我正在谈论的问题。这是提供关于我的问题的 100% 信息的唯一方法。"

如果您不能使用 ID 和值 - 使用任何其他属性。在你的情况下 'data-scv-reaction-id' 是一个不错的选择)

//*[@data-scv-reaction-id='strongly-disagree1']

以下是我用来保持 xpath 简洁且更易于更改的一些技巧。

  1. 您可以通过将 .. 添加到 xpath 的末尾来引用比您想要 select 的元素更深的元素。例如,我会使用 <i class="x" /> 来区分要单击的内容,然后使用 .. 来 select 父级 <label />

  2. 您可以在 xpath 的中间使用 // 作为通配符来浏览不感兴趣的元素。

例如:By.xpath("//form[@id='questionform']//i[@class='sc-strongly-disagree']/..")

所以让我们这样理解:

  1. 每个问题都有一个反应部分。
  2. 每个反应部分都有一个多重反应buttons/icons。

我遇到过这样的情况,需要按以下方式处理:

public void reactToQuestion(String question, String reaction) {
    // Get the question element
    // For eg. question = "I always complete all aspects of my work."
    By locQuestion = By.xpath("//*[text()='" + question + "']");
    WebElement element = driver.findElement(locQuestion);

    // Get the parent 'div' container of the question
    WebElement container = element.findElement("..");

    // Get reaction RELATIVELY to the question
    // reaction = 'strongly-disagree1', 'slightly-disagree1', etc.
    By locReaction = By.xpath(".//*[@class='inputs']/input[@data-scv-reaction-id=" + reaction + "]");
    WebElement reaction = container.findElement(locReaction);
    reaction.click();

}

如果有任何问题,请尝试告诉我。

祝你好运。

在这种情况下,您要做的是找到一个 "container" 元素,其中包含您想要 select 的所有选项。在这种情况下,我们可以使用问题文本使用 XPath 找到包含所有 5 个选项的 DIV。从那里,您可以根据 I 标签的 class 名称 select 所需的选择,例如<i class="sc-strongly-agree">。因此,只要提供问题文本和所需的选择,我们就可以 selection.

在这种情况下,您可能会一遍又一遍地使用相同的代码,您通常需要创建一个函数来处理这个问题。此外,由于您 select 反复选择 5 个选项之一,我更喜欢使用枚举。您可以使用 String,但您必须处理可能存在的各种输入,如果有人拼错或以某种方式打错了字符串,脚本就会失败。枚举使 select 所需的选择变得容易,因为 IDE 总是向您显示选择,这使它变得轻而易举。如果你打错了枚举,它就不会编译……如果脚本会失败,最好早点知道。枚举也是这些选择的容器,因此如果 class 名称发生变化,您更改枚举中的 class 名称并且所有脚本都是固定的。

函数

public static void selectChoice(String question, Choices choice)
{
    driver.findElement(By.xpath("//div[contains(@class,'happiness-reaction__item')][contains(.,'" + question + "')]//i[@class='" + choice.value() + "']")).click();
}

枚举

public enum Choices
{
    STRONGLYDISAGREE("sc-strongly-disagree"),
    SLIGHTLYDISAGREE("sc-slightly-disagree"),
    NONE("sc-none"),
    SLIGHTLYAGREE("sc-slightly-agree"),
    STRONGLYAGREE("sc-strongly-agree");

    private String value;

    Choices(String value)
    {
        this.value = value;
    }

    public String value()
    {
        return value;
    }
}

这是你如何称呼它

selectChoice("I often get upset over minor issues.", Choices.STRONGLYAGREE);

作为一个通用的 xpath,首先从问题中输入,它可能是这样的:

//form[@id='questionform']/div//input

如果您需要任何 input 的特定问题:

//form[@id='questionform']/div[contains(@class, 'happiness-reaction')]//input

如果您需要某个问题的特定答案,那么:

//form[@id='questionform']/div[contains(@class, 'happiness-reaction')]//input[@data-scv-reaction-id='strongly-disagree1']

如果您需要为问题 and/or 添加参数,请回答:

//form[@id='questionform']/div[contains(@class, 'part_of_question_class')]//input[@data-scv-reaction-id='desired_answer_attr']

这些参数可以以 array/list 或您喜欢的任何形式存储。

所有这些都可以在 css 中完成,另外:

form#questionform>div input
form#questionform>div[class*=happiness-reaction] input
form#questionform>div[class*=happiness-reaction] input[data-scv-reaction-id='strongly-disagree1']

你懂的。