【问题标题】:Couldn't locate an error message in Selenium to assert在 Selenium 中找不到要断言的错误消息
【发布时间】:2018-10-03 12:49:30
【问题描述】:

我试图编写测试来验证必填字段。其中一个字段是“文档 ID”。我找到了它的 XPath 并检查了 firefox 和 chrome 并且它工作正常。当我在脚本中使用相同的内容时,我得到了 NoSuchElementException。元素的代码在:

<div class="col-xs-12 col-sm-6 edit-view-row-item">
<div class="col-xs-12 col-sm-4 label" data-label="LBL_RESUME_ID">
Document ID:
<span class="required">*</span>
</div>
<div class="col-xs-12 col-sm-8 edit-view-field " field="resume_id" 
type="varchar">
<input id="resume_id" type="text" title="" value="" maxlength="255" size="30" 
 name="resume_id" tabindex="0" style="background-color: rgb(255, 255, 255);">
<label>
<input id="document_type_c" type="radio" title="" checked="checked" 
value="Passport" name="document_type_c" tabindex="0">
Passport
</label>
<label>
<input id="document_type_c" type="radio" title="" value="DriversLicense" 
name="document_type_c" tabindex="0">
Driver's License
</label>
<label>
<input id="document_type_c" type="radio" title="" value="EAD" 
name="document_type_c" tabindex="0">
EAD Copy
</label>
<label>
<input id="document_type_c" type="radio" title="" value="State_ID" 
name="document_type_c" tabindex="0">
State ID
</label>
<label>
<input id="document_type_c" type="radio" title="" value="Others" 
name="document_type_c" tabindex="0">
Others
</label>
<div class="required validation-message">Missing required field: Document 
ID</div>
</div>
</div>

我尝试过的 XPath:

//input[@id='resume_id']/../div
//input[@value='Others']/../following-sibling::div

我试过的代码:

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement dIdErrorMessage = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[@id='resume_id']/../div")));

WebElement dIdErrorMessage = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[@value='Others']/../following-sibling::div")));

String validityMessage = dIdErrorMessage.getText();
Assert.assertEquals("Missing required field: Document ID", validityMessage);

【问题讨论】:

  • 您是否尝试wait 让所需元素出现在 DOM 中?
  • 发布您尝试过的不同代码尝试。元素是否在 IFRAME 中?您是否按照安德森的建议尝试了等待?
  • 是的。我确实等待元素@Andersson 的可见性。仍然得到 NoSuchElementException。
  • 没有。该元素不在框架中@JeffC。
  • @learningQA ,你确定吗?如果你申请了等待可见性但它失败了,你应该得到TimeoutException,而不是NoSuchElementException

标签: java selenium selenium-webdriver xpath webdriver


【解决方案1】:

尝试直接检查元素内的文本:

//div[text()[normalize-space()='文档ID:']]

如果这不是解决方案,请指定您希望验证的元素或文本。

【讨论】:

    猜你喜欢
    • 2020-09-04
    • 1970-01-01
    • 2016-10-21
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2019-04-15
    • 1970-01-01
    相关资源
    最近更新 更多