【问题标题】:selenium webdriver find element in regionselenium webdriver在区域中查找元素
【发布时间】:2015-04-16 09:19:38
【问题描述】:

在使用自动化测试时,我经常遇到以下问题:我想在页面上找到一个元素,但该元素必须位于页面的特定区域。

以以下为例: 我在网站上有一个预先输入的搜索字段。在我的侧边栏中,我有我正在搜索的元素(我们称之为“Selenium”),但这不是我感兴趣的元素,我想看看我的预输入搜索在搜索时是否提供了预期的结果“硒”。

<body>
   <header>
      <searchfield>
         <searchresults>
            <a .... >Selenium</a>
         <searchresults>
      </searchfield>
   </header>
   <aside>
      ...
      <a .... >Selenium</a>
      ...
   </aside>
</body>

如果我在 selenium 网络驱动程序中搜索链接文本“Selenium”,它将找到两个条目,侧边栏中的条目以及搜索字段中的条目。 此外,我无法等待使用链接文本上的WaitForVisible 命令的搜索结果,因为 Selenium 会在侧边栏中找到该元素并得出该元素已预设的结论。

所以我的问题是: “使用 selenium webdriver,我如何在特定区域内搜索元素?”

【问题讨论】:

    标签: selenium xpath selenium-webdriver


    【解决方案1】:

    围绕这个问题,我发现了 Xpath 的使用。有了这个,我可以创建我想要搜索元素的“区域”。例如,我从

    html/body/div/aside/div[2]/ul/li
    

    //div[contains(@class,'coworkerwidget')]/ul/li
    

    现在代码更加动态,如果我们的前端人员将来编辑某些内容,代码更不容易出错。

    关于搜索,我现在可以设置如下代码

    //div[contains(@class, 'searchfield')]//div[contains(@title, 'searchfield') and contains(., '" + searchword + "')]"
    

    首先我们指定要在搜索区域中查找:

    //div[contains(@class, 'searchfield')]
    

    然后我可以为我想要查找的结果设置更多标准:

    //div[contains(@class, 'title') and contains(., '" + searchword + "')]
    

    一些关于该主题的文献供进一步阅读。

    http://www.qaautomation.net/?p=388

    http://www.w3schools.com/xsl/xpath_syntax.asp

    Click a button with XPath containing partial id and title in Selenium IDE

    Retrieve an xpath text contains using text()

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 2017-09-12
      • 2021-03-31
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 2021-07-04
      • 2014-08-13
      相关资源
      最近更新 更多