【问题标题】:How to determine if an element is displayed on screen for specific time using selenium java/python?如何使用 selenium java/python 确定元素是否在特定时间显示在屏幕上?
【发布时间】:2022-01-10 23:01:26
【问题描述】:

有一个网页元素(成功或失败消息)仅在屏幕上显示 3 秒。如何使用 selenium java 或 python 确保元素在屏幕上显示 3 秒?

【问题讨论】:

    标签: java python-3.x selenium-webdriver automation


    【解决方案1】:

    您可以将java.time.LocalDateTimejava.time.temporal.ChronoUnitorg.openqa.selenium.support.ui.ExpectedConditions 结合使用。

    在此示例中,测量按钮未启用的时间:

    package selenium;
    
    import java.time.LocalDateTime;
    import java.time.temporal.ChronoUnit;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class DeviTimeTest {
    
        public static String userDir = System.getProperty("user.dir");
        public static String chromedriverPath = userDir + "\\resources\\chromedriver.exe";  
        public static WebDriver driver;
        
        public static void main(String[] args) {
            WebDriver driver = startChromeDriver();
            driver.get("https://demoqa.com/dynamic-properties");
            LocalDateTime start = LocalDateTime.now();
            WebElement button = driver.findElement(By.id("enableAfter"));
            new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(button));
            LocalDateTime finish = LocalDateTime.now();
            System.out.println("Button was disabled for " + ChronoUnit.MILLIS.between(start, finish) + " ms.");
            driver.quit();
        }
    
        public static WebDriver startChromeDriver() {
            System.setProperty("webdriver.chrome.driver", chromedriverPath);
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--ignore-certificate-errors");
            options.addArguments("--start-maximized");
            options.addArguments("--disable-notifications");
            driver = new ChromeDriver(options);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            return driver;
        }
    }
    

    输出:

    Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 47769
    Only local connections are allowed.
    Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
    ChromeDriver was started successfully.
    Pro 06, 2021 8:00:55 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Button was disabled for 4404 ms.
    

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 2013-02-15
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      相关资源
      最近更新 更多