6

我有一个包含以下内容的 html 页面:

    <center> 
          This is Login page. 
          <br> 
          Please click below link to Login.
          <br> 
          <a href="xxx">Login</a> 
    </center>

如何使用一个 webdriver 命令验证上述所有静态文本?

我尝试了这些,但都不起作用:(

driver.findElement(By.xpath("//*[contains(.,'This is Login page.<br>Please click below link to Login')]"))
driver.findElement(By.xpath("//*[contains(.,'This is Login page.\nPlease click below link to Login')]"))

有人知道该怎么做吗?

4

4 回答 4

6

你可以做:

webDriver.findElement(By.id("<ID of center tag>")).getText().contains("This is Login page.Please click below link to Login.Login");

随意使用您选择的定位器而不是 By.id。

基本上任何 WebElement 的 getText() 都会返回节点的文本内容,去掉所有的 HTML 标记和注释。

于 2012-05-22T08:50:03.260 回答
1

这比 Selenium 更像是 XPath 的限制。

我不明白你为什么在那里使用全局 XPath 选择器,更好的 XPath 选择器应该是:

By.XPath("//center");

如果它是页面上唯一的“中心”标签,甚至:

By.XPath("//center[contains(text(), '这是一个登录页面'")

如果您通过代码而不是在 XPath 中盲目地搜索它,那么 /r 和 /n 字符将被保留。

于 2012-05-22T09:02:11.773 回答
-1

How to find the texts if the texts has space inbetween them.

      <br> 
        <br/>
      Please click below link to Login.
      <br> 
      <a href="xxx">Login</a> 
</center>
于 2013-11-26T19:19:35.507 回答
-1

您可以使用“\n”

return getText(supportText).contains("Information:" + "\n" +
    "This page is for use by the TECHNICAL TEAM only. Following links will be active, only if you have access (subscription) to those modules.");
于 2018-10-30T21:10:43.497 回答