【问题标题】:Find attribute values in Selenium Webdriver在 Selenium Webdriver 中查找属性值
【发布时间】:2014-12-17 23:16:00
【问题描述】:

我是 Selenium 2.0 和 WebDriver 的新手,欢迎任何有关如何解决我的问题的帮助和建议。

我正在查看的html代码是:

<html>
<head>
<body class="index">
    <div class="container">
    <div class="masthead">
    <div class="status-bar">
    <div class="app-vpn-selector-container">
    <div id="page_tabs" class="tabs">
    <div class="tab-group monitoring">
    <div class="tab-group-name"> Monitoring </div>
    <div class="tab active"> Status </div>
    <div class="tab"> Events </div>
    <div class="tab"> Reports </div>
    <div class="tab"> Statistics </div>
    </div>
    <div class="tab-group configuration">
    </div>
    <div id="page_content" class="tab_content">
    </div>
 </body>
</html>

我的任务是存储选项卡的所有名称:StatusEventsReportsStatistics em> 并断言这些值存在。

List<WebElement> allTabs = webdriver1.findElements(By.xpath("//div[@id='page_tabs']/div/div"));

会让我找到元素,但我无法读取属性值。 有人可以让我知道如何解决它。 我花了很多时间在网上阅读信息,但找不到解决方案。 提前致谢

【问题讨论】:

    标签: selenium-webdriver


    【解决方案1】:

    findElements 返回一个列表。它会根据您的 xpath 为您提供所有二级 div。获取列表后,您需要做的是遍历元素并调用 getText 方法并验证您获取的文本..效果

     List<WebElement> lstElements = driver.findElementsByXPath("//div[@id='page_tabs']/div/div");
    
       for (WebElement e : lstElements){
            //Verify e.getText() matches the expected value
        }
    

    【讨论】:

      【解决方案2】:

      嘿,如果您在阅读文本(选项卡名称)时遇到问题...那么 getText() 就是您正在寻找的方法...

      String K= Driver.findElements(By.xpath("Main tab Exp")).FindElement(By.xpath("Tab Exp")).getText();
      

      我希望这能解决问题。一切顺利:-)

      【讨论】:

        猜你喜欢
        • 2018-09-01
        • 1970-01-01
        • 2015-05-06
        • 2012-12-26
        • 2014-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多