【问题标题】:Pls how to access message in span tag请如何访问跨度标签中的消息
【发布时间】:2014-01-02 08:56:46
【问题描述】:
<html>
<body>
<section id="click" class="content mCustomScrollbar _mCS_1">
<div id="mCSB_1" class="mCustomScrollBox mCS-dark" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
<div class="mCSB_container" style="position:relative; top:0;">
<form id="registration" action="http://abcd.com/setup/user-registration/index" method="post" name="registration">
<article class="frm-content">
<div class="leftsection">
<div class="form-element">
<fieldset>
<label>
<input id="firstname" type="text" maxlength="50" value="" name="firstname" style="border: 1px solid red;">
    <label>
<span class="required">*</span>
First Name:
</label>
<input id="firstname" type="text" maxlength="50" value="" name="firstname"     style="border: 1px solid red;">
<span id="error_firstname" class="errormsg" role="alert">Please use only letters,numbers, period, single space and underscore</span>
</fieldset>
</article>
</div>
</section>
</body>
</html>

I 以上代码我要查找请仅使用字母、数字、句点、单个空格和下划线消息。它的 xpath 是 /html/body/section[2]/div/div/form/article/div/div/span 但它没有被 gettext()访问> 方法。 请问如何访问????

【问题讨论】:

  • by xpath //span[@id='error_firstname'] by css #error_firstname by id error_firstname 最后是getText()
  • 该元素可访问,但 getText() 未获取错误消息
  • 检查 Driver.FindElement(By.id("error_firstname")).isDisplayed() 在使用 getText() 方法之前返回 true。因为它获取元素的可见(即不被 CSS 隐藏)innerText。
  • 谢谢 Anuragh27crony 它的工作......

标签: selenium selenium-webdriver


【解决方案1】:

getText() 方法仅获取相应元素的可见(即不被 CSS 隐藏)innerText。这可能是问题的原因。

请尝试以下检查是否显示元素。

Driver.FindElement(By.id("error_firstname")).isDisplayed();

您可以添加 WebDriverwait 直到显示该元素,然后从该元素中获取文本。

WebDriverWait Driver_Wait=new WebDriverWait(Driver, 10); //10 denotes Total_Secs to wait
Driver_Wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("error_firstname")));

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    通过使用 id “error_firstname”,我们只能获取 id 为 error_firstname 的 span 中的值..
    所以使用最有效的 id "click"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多