Selenium - 无法按类名定位元素

时间:2014-08-28 15:29:25

标签: java selenium-webdriver

我想找到一个p代码class = "big-number"。这是我写的代码:

WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("big-number")));
System.out.println(driver.getTitle());
System.out.println(myDynamicElement);

这是我的输出:

[[FirefoxDriver: firefox on MAC (fed46ad4-9ca9-9344-a57a-1d336db3927c)] -> class name: big-number]

我无法识别错误,它给了我一个输出,但它对我没有意义。 关于如何至少识别我的错误的任何提示?

我当然确定元素存在,这是HTML代码:

    <div id="users-online-container" style="">
        <img class="big-number-icon" src="images/usersOnline.png">
        <p class="big-number">228</p>
        <p class="caption">Users Online</p>
    </div>
    <div id="users-online-loading"></div>

2 个答案:

答案 0 :(得分:0)

发生TimeOutException是因为驱动程序在特定时间内找不到元素。选择中的问题我认为。如果您确定该元素始终可见,并且存在于页面上,请尝试下一个代码:

//Select first paragraph in div
driver.FindElement(By.CssSelector("#users-online-container .big-number"));
//if you have several p with same classes you could access any of them using index. e.g.
driver.findElements(By.CssSelector(".big-number"))[index];

选择器可以是#users-online-container .big-number.big-number。两者都有效。

答案 1 :(得分:0)

尝试以下代码..

Dispose

此外,您尝试在XPATH的帮助下找到该元素,并确保您的定位器唯一标识该元素。还要检查IsDisplayed()和IsEnabled()是否返回True。

在您的代码中,您正在打印将打印Hashcode的WebElement。 为了获得Element的文本,你必须使用getText()方法。

希望它会有所帮助!

相关问题