【问题标题】:how to perform click on an element using selenium for which i am getting StaleElementReferenceException如何使用我正在获取 StaleElementReferenceException 的 selenium 执行单击元素
【发布时间】:2019-05-05 16:00:16
【问题描述】:

问题:有几个具有相同 ID 的单选按钮,我需要单击这些按钮。第一个单选按钮获得点击,第二个单选按钮获得StaleElementReferenceException

任何人都可以建议我如何处理它,以便当我单击第二个单选按钮时它不会抛出 StaleElementReferenceException

HTML:

<solvup-radio-type _ngcontent-c7="" _nghost-c10=""><div _ngcontent-c10="" class="validation-error ng-untouched ng-pristine ng-invalid">

    <solvup-label _ngcontent-c10="" _nghost-c33=""><label _ngcontent-c33="" class="control-label">
     Have you been able to resolve the issue based on this information? <!----><span _ngcontent-c33="" class="required-mark">*</span> 
</label>
<solvup-tooltip _ngcontent-c33="" _nghost-c36=""><!---->
</solvup-tooltip>
</solvup-label>

  <!----><div _ngcontent-c10="" class="radio">
      <label _ngcontent-c10="">
          <input _ngcontent-c10="" type="radio" id="ts_note_questions" class="ng-untouched ng-pristine ng-invalid">
          Yes, issue is resolved - close case
      </label>

  </div><div _ngcontent-c10="" class="radio">
      <label _ngcontent-c10="">
          <input _ngcontent-c10="" type="radio" id="ts_note_questions" class="ng-untouched ng-pristine ng-invalid">
          No - continue to next step
      </label>

  </div>
</div>
<solvup-hint-text _ngcontent-c10="" _nghost-c34=""><!----><p _ngcontent-c34="" class="help-block"></p>
</solvup-hint-text>
<solvup-validation-messages _ngcontent-c10="" _nghost-c35=""><!---->



<!----><div _ngcontent-c35="" class="form-group">
     <small _ngcontent-c35="" class="err"><i _ngcontent-c35="" aria-hidden="true" class="fa fa-exclamation-triangle"></i>  Please select an option</small>
</div>

</solvup-validation-messages>


</solvup-radio-type>

硒代码:

 @Given("^User fills details in First of Five Troubleshooting  page$")
public void user_fills_details_in_First_of_Five_Troubleshooting_page() throws Throwable {
    Thread.sleep(2000);
    List<WebElement> li = driver.findElements(By.className("radio"));
    Actions ob = new Actions(driver);
    ob.moveToElement(li.get(1));
    ob.click(li.get(1));
    Action action = ob.build();
    action.perform();   
}

请提出建议。

【问题讨论】:

  • StaleElement 通常在执行操作时重新加载页面的 dom 时出现。要解决此问题,您可以等待几秒钟重新加载页面然后重新定位 Web 元素并执行操作跨度>
  • @ShekharSwami 我应用了隐式等待。但它不工作
  • 隐式不起作用.. Thread.sleep
  • @ShekharSwami 正如您在代码中看到的那样,我已经在使用 thread.sleep 但它也不起作用

标签: selenium selenium-webdriver automation ui-automation browser-automation


【解决方案1】:

查看 dom 结构,您可能正在尝试单击可能不正确的元素。我想

List<WebElement> li = driver.findElements(By.Id("ts_note_questions"));
Actions ob = new Actions(driver);
ob.moveToElement(li.get(1));
action.perform();  
li.get(1).click();

会起作用,因为您尝试点击输入而不是 div。

【讨论】:

    【解决方案2】:

    我注意到您复制了第一个单选按钮,但您似乎忘记更改第二个单选按钮的 ID。尝试将第二个单选按钮的 id 更改为唯一的。

    【讨论】:

    • 您的意思是将第二个单选按钮的 id 更改为 html 中唯一的东西吗?如果这是我做不到的。请用现有的html提出建议
    • @shreya_bhatngr - xpath 索引不起作用?? (//input[@type='radio'])[1] - 指向第一个单选按钮 (//input[@type='radio'])[2] - 指向第二个单选按钮 (//input[@type='radio'])[3] - 指向第三个单选按钮
    猜你喜欢
    • 2019-10-23
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多