0

ExpectedConditions.invisibilityOfElementLocated throws Timeout Exception并终止程序。

  1. 启动浏览器并导航到登录页面。

  2. 输入用户名、密码,点击登录

  3. 由于加载需要不同的时间,我想等到登录按钮不可见

这适用于我使用有效凭据并单击登录的积极情况。但对于负面情况,它总是失败,即当我在登录时尝试使用无效的用户名和密码组合 clcik 时,它正在等待最长时间,然后因超时异常而失败。最后我尝试使用一个Try / Catch经过验证的标题,但真的不明白为什么它不能在负面情况下工作,尽管等待条件是登录页面并且我正在使用不可见的元素......

driver = new FirefoxDriver();
    driver.manage().window().maximize();
    //driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
    driver.get("http://newtours.demoaut.com/");
    driver.findElement(By.name("userName")).sendKeys("valid password");
    driver.findElement(By.name("password")).sendKeys("invalid password");
    driver.findElement(By.name("login")).click();
    WebDriverWait wait = new WebDriverWait(driver, 60);
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.name("login")));
    titleActual = driver.getTitle();
    if (titleActual.equals(titleExpected)) {
        System.out.println("Login successful");
    } 
    else {
}
driver.close;

预期:输入用户名和密码后,一旦我点击登录 - 它应该退出等待并获取驱动程序页面标题实际:它正在等待给出的最大时间错误超时异常,终止程序执行

日志:

2019 年 8 月 21 日下午 5:38:37 org.openqa.selenium.remote.ProtocolHandshake createSession INFO:检测到方言:线程“主”org.openqa.selenium.TimeoutException 中的 W3C 异常:预期条件失败:等待元素不再可见:By.name:登录(尝试 60 秒,间隔 500 毫秒)构建信息:版本:'3.141.59',修订:'e82be7d358',时间:'2018-11-14T08:17:03 '系统信息:主机:'LAPTOP-2PL6I9SD',ip:'192.168.43.30',os.name:'Windows 10',os.arch:'amd64',os.version:'10.0',java.version:' 11.0.1' 驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 67.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID:20190618025334, moz:geckodriverVersion: 0.24.0, moz:headless: false, moz:processID: 15188, moz:profile: C:\Users\SWATHI\AppData\Loc..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000 },unhandledPromptBehavior:dismiss and notify} 会话 ID:在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95) 在 org.openqa.selenium.support 上的 e2401e63-ffda-45cd-a73f-ea418e38236d。 ui.FluentWait.until(FluentWait.java:272) 在 basic_tests.LoginFlightTest.main(LoginFlightTest.java:33)假,moz:processID:15188,moz:profile:C:\Users\SWATHI\AppData\Loc ...,moz:shutdownTimeout:60000,moz:useNonSpecCompliantPointerOrigin:假,moz:webdriverClick:true,pageLoadStrategy:正常,平台: WINDOWS,platformName:WINDOWS,platformVersion:10.0,rotatable:false,setWindowRect:true,strictFileInteractability:false,超时:{implicit:0,pageLoad:300000,脚本:30000},unhandledPromptBehavior:dismiss and notify}会话ID:e2401e63-ffda -45cd-a73f-ea418e38236d at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272) at basic_tests .LoginFlightTest.main(LoginFlightTest.java:33)假,moz:processID:15188,moz:profile:C:\Users\SWATHI\AppData\Loc ...,moz:shutdownTimeout:60000,moz:useNonSpecCompliantPointerOrigin:假,moz:webdriverClick:true,pageLoadStrategy:正常,平台: WINDOWS,platformName:WINDOWS,platformVersion:10.0,rotatable:false,setWindowRect:true,strictFileInteractability:false,超时:{implicit:0,pageLoad:300000,脚本:30000},unhandledPromptBehavior:dismiss and notify}会话ID:e2401e63-ffda -45cd-a73f-ea418e38236d at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272) at basic_tests .LoginFlightTest.main(LoginFlightTest.java:33)useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script :30000},unhandledPromptBehavior:dismiss and notify} 会话 ID:在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95) 在 org.openqa.selenium 的 e2401e63-ffda-45cd-a73f-ea418e38236d。 support.ui.FluentWait.until(FluentWait.java:272) 在 basic_tests.LoginFlightTest.main(LoginFlightTest.java:33)useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script :30000},unhandledPromptBehavior:dismiss and notify} 会话 ID:在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95) 在 org.openqa.selenium 的 e2401e63-ffda-45cd-a73f-ea418e38236d。 support.ui.FluentWait.until(FluentWait.java:272) 在 basic_tests.LoginFlightTest.main(LoginFlightTest.java:33){implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior:dismiss and notify} 会话 ID:e2401e63-ffda-45cd-a73f-ea418e38236d 在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java :95) 在 org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272) 在 basic_tests.LoginFlightTest.main(LoginFlightTest.java:33){implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior:dismiss and notify} 会话 ID:e2401e63-ffda-45cd-a73f-ea418e38236d 在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java :95) 在 org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272) 在 basic_tests.LoginFlightTest.main(LoginFlightTest.java:33)

4

2 回答 2

0

我已经验证了您的解决方案,这可能是您的 n/w 或站点没有快速响应的问题。您的脚本运行正常,没有任何问题。请使用以下脚本再试一次,不要忘记更新 chrome 驱动程序路径。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Sampletest {
    public static void main(String[] args) {


    System.setProperty("webdriver.chrome.driver", "C:\\Users\\win32\\76.0.3809.68\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    //driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
    driver.get("http://newtours.demoaut.com/");
    driver.findElement(By.name("userName")).sendKeys("myworld");
    driver.findElement(By.name("password")).sendKeys("myworld");
    driver.findElement(By.name("login")).click();
    WebDriverWait wait = new WebDriverWait(driver, 60);
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.name("login")));
    String titleActual = driver.getTitle();
    if (titleActual.equals("titleExpected")) {
        System.out.println("Login successful");
    } else {
    }
    driver.close();
}
}
于 2019-10-29T12:06:59.243 回答
0

这种方法会失败,因为有时网络延迟可能很长并且登录按钮会消失。但是,尚未加载下一页。所以正面案例也可能失败。您应该尝试等待登录后加载的下一页上的某些组件。

在否定测试用例中,您可以等待显示相应的错误消息。

于 2019-08-21T15:50:38.183 回答