如何使用 Selenium 监控网页并知道何时出现新的 html 元素?

我有一个网页,有时会动态添加新元素,例如:


<span class="chat_message">| New Login</span>

将上述代码添加到我的页面时如何捕获?


我的代码试验:


WebDriver driver = new ChromeDriver () ;

 driver.get("http://www.example.com") ;

// code to monitor the new span


慕尼黑8549860
浏览 608回答 3
3回答

哔哔one

请使用下面的代码片段。如果没有找到匹配的元素而不是异常,findElements 将返回一个空列表,但是我已经完成了异常处理。import java.awt.AWTException;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.testng.annotations.Test;public class Testing {&nbsp; &nbsp; &nbsp; &nbsp;public static WebDriver driver;&nbsp; &nbsp; &nbsp; &nbsp;@Test&nbsp; &nbsp; &nbsp; &nbsp;public void test() throws InterruptedException, AWTException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WebDriver driver = new ChromeDriver();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; driver.get("http://www.example.com");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Boolean isPresent = driver.findElements(By.xpath("//span[@class='chat_message']")).size() > 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (isPresent == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("New Login is added to my page");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("New Login is not added to my page");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}}如果至少找到一个元素,则返回 true,如果不存在则返回 false。如果它符合您的期望,请接受答案并进行投票。提前致谢。

largeQ

当您提到您需要捕获元素时,用例归结为使用ExpectedConditions诱导WebDriverWait,以便您可以提取任何元素属性:visibilityOfElementLocated(By locator)内部HTML类属性外层HTML在这些情况下,最好的选择是创建如下函数:public void getElementAttribute(){&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='chat_message']"))).getAttribute("innerHTML"));&nbsp; &nbsp; }&nbsp; &nbsp; catch(Exception TimeoutException) {&nbsp; &nbsp; &nbsp; System.out.println("Element no found");&nbsp; &nbsp; }}现在,您可以从程序中的任何位置调用此函数来检查元素的可见性,如下所示:getElementAttribute();笔记:由于元素是动态元素,您需要诱导WebDriverWait。由于您需要捕获元素visibilityOfElementLocated非常适合。有时您需要将代码行包装在一个try-catch {}块中,并在出现异常的情况下优雅地处理并TimeoutException继续您的下一步。

喵喵时光机

如果您知道该元素的定位器 - 有一个“while”循环,其中包含 afindElement()并捕获NoSuchElementException.&nbsp;如果该元素不存在,您将捕获异常,暂停一段时间(尽管如此sleep),然后开始一个新的循环周期。如果没有抛出异常,则元素存在;将您的 while 控制变量更改为true,然后继续。我建议有一个计数器循环运行了多少次,如果它达到某个阈值 - 打破它,出现错误/异常 - 这样你就不会陷入无限循环。恭喜 - 您刚刚WebDriverWait使用presenceOfElementLocated()ExpectedConditions 实施。您可以使用它(香草硒版本),或坚持使用本土解决方案,这将为您提供更精细的控制和决策树 - 但需要更多编码。如果你没有具体的元素,只是想看看页面本身什么时候发生变化,算法是一样的,但是:在开始循环之前,先获取页面源。在它的体内,再次得到它;如果两者不同,那就是你的突破条件。这种方法虽然会受到页面中最轻微的变化的影响。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java