如何使用我正在获取StaleElementReferenceException的硒执行元素单击

时间:2018-12-04 09:14:03

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

问题:有几个具有相同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();   
}

请提出建议。

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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