【问题标题】:How to monitor a web page and know when a new html element is appeared using Selenium?如何使用 Selenium 监控网页并知道何时出现新的 html 元素?
【发布时间】:2019-02-17 02:40:06
【问题描述】:

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

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

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

我的代码试验:

WebDriver driver = new ChromeDriver () ;
 driver.get("http://www.example.com") ;
// code to monitor the new span

【问题讨论】:

标签: java selenium selenium-webdriver webdriver webdriverwait


【解决方案1】:

如果您知道该元素的定位器 - 有一个“while”循环,其中包含 findElement(),并捕获 NoSuchElementException。如果元素不存在,您将捕获异常,暂停一段时间(尽管sleep),然后开始一个新的循环周期。如果没有抛出异常,则元素存在;将您的 while 控制变量更改为 true,然后继续。

我建议有一个计数器,循环运行了多少次,如果它达到某个阈值 - 打破它,出现错误/异常 - 这样你就不会陷入无限循环。

恭喜 - 您刚刚使用 presenceOfElementLocated() 预期条件实现了 WebDriverWait。您可以使用它(香草硒版本),或坚持使用本土解决方案,这将为您提供更精细的控制和决策树 - 但需要更多编码。


如果你没有具体的元素,只是想看看页面本身什么时候发生变化,算法是一样的,但是:在开始循环之前,先获取页面源。在它的身体里,再次得到它;如果两者不同,那就是你的突破条件。
这种方法虽然会受到页面中最轻微的变化的影响。

【讨论】:

    【解决方案2】:

    请使用以下代码 sn-p。 如果没有找到匹配的元素而不是异常,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 {
    	public static WebDriver driver;
    
    	@Test
    	public void test() throws InterruptedException, AWTException {
    		WebDriver driver = new ChromeDriver();
    		driver.get("http://www.example.com");
    		Boolean isPresent = driver.findElements(By.xpath("//span[@class='chat_message']")).size() > 0;
    		try {
    			if (isPresent == true) {
    				System.out.println("New Login is added to my page");
    			} else {
    				System.out.println("New Login is not added to my page");
    			}
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }

    如果至少找到一个元素,则返回 true,如果不存在,则返回 false。

    如果它符合您的期望,请接受答案并投赞成票。提前致谢。

    【讨论】:

      【解决方案3】:

      当您提到您需要捕获元素时,用例归结为以 ExpectedConditions 诱导 WebDriverWait visibilityOfElementLocated(By locator) 这样您就可以提取任何元素属性:

      • innerHTML
      • 属性
      • 外层HTML

      在这些情况下,最好的选择是创建如下函数:

      public void getElementAttribute()
      {
          try {
            System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='chat_message']"))).getAttribute("innerHTML"));
          }
          catch(Exception TimeoutException) {
            System.out.println("Element no found");
          }
      }
      

      现在,您可以在程序中的任何位置调用此函数来检查元素的可见性,如下所示:

      getElementAttribute();
      

      注意:

      • 由于元素是动态元素,您需要诱导WebDriverWait
      • 当您需要捕获元素时,visibilityOfElementLocated 非常适合。
      • 碰巧有时您需要将代码行包含在try-catch {} 块中,并在出现异常时优雅地处理TimeoutException strong> 并继续下一步。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-21
        相关资源
        最近更新 更多