无法在Selenium中找到要声明的错误消息

时间:2018-10-03 12:49:30

标签: java selenium selenium-webdriver xpath webdriver

我试图编写测试以验证必填字段。字段之一是“文档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);

1 个答案:

答案 0 :(得分:0)

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

  

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

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