java - wait.until(ExpectedConditions.visibilityOf(userName)) 不起作用

标签 java selenium

在下面的第 33 行代码中,在 wait.until 附近出现以下错误消息

该行有多个标记 - 无法解析 com.google.common.base.Function 类型。它是从所需的 .class 文件间接引用的 - FluentWait类型中的方法until(Function)不适用于 参数(预期条件)

   package MPA;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginhelperClass {

    WebDriver driver;

    @FindBy(xpath = "//input[@ng-model='login.username']")
    private WebElement userName;

    @FindBy(xpath = "//input[@type='password']")
    private WebElement Password;

    @FindBy(xpath = "//input[@type='submit' and @value='Sign In']")
    private WebElement SignIn;

    @FindBy(xpath = "//span[@class='icon-avatar-round']")
    private WebElement Avatar;

    public LoginhelperClass(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void logIn(String uName, String Pswd) throws InterruptedException {
        WebDriverWait wait=new WebDriverWait(driver,7);
        wait.until(ExpectedConditions.visibilityOf(userName));
        userName.sendKeys(uName);
        Password.sendKeys(Pswd);
        SignIn.click();
        try {
            Thread.sleep(4000);
            if (Avatar.isDisplayed()) {
                System.out.println("Login Successfull: " + uName);
            } else {
                System.out.println("Login failure check the credentials: " + uName);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}




 package MPA;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginhelperClass {

    WebDriver driver;

    @FindBy(xpath = "//input[@ng-model='login.username']")
    private WebElement userName;

    @FindBy(xpath = "//input[@type='password']")
    private WebElement Password;

    @FindBy(xpath = "//input[@type='submit' and @value='Sign In']")
    private WebElement SignIn;

    @FindBy(xpath = "//span[@class='icon-avatar-round']")
    private WebElement Avatar;

    public LoginhelperClass(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void logIn(String uName, String Pswd) throws InterruptedException {
        WebDriverWait wait=new WebDriverWait(driver,7);
        wait.until(ExpectedConditions.visibilityOf(userName));
        userName.sendKeys(uName);
        Password.sendKeys(Pswd);
        SignIn.click();
        try {
            Thread.sleep(4000);
            if (Avatar.isDisplayed()) {
                System.out.println("Login Successfull: " + uName);
            } else {
                System.out.println("Login failure check the credentials: " + uName);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

最佳答案

您看到的错误如下:

Multiple markers at this line - The type com.google.common.base.Function cannot be resolved. It is indirectly referenced from required .class files - The method until(Function) in the type FluentWait is not applicable for the arguments (ExpectedCondition)

让我们分析一下代码中发生了什么。

页面对象中,您已将WebElement定义为:

@FindBy(xpath = "//input[@ng-model='login.username']")
private WebElement userName;

继续前进,您已尝试在方法中使用 WebElement :

public void logIn(String uName, String Pswd) throws InterruptedException {
    WebDriverWait wait=new WebDriverWait(driver,7);
    wait.until(ExpectedConditions.visibilityOf(userName));
    //
}

所以基本上我们在这里试图等待间接引用 Angular WebElement userName 的可见性取决于属性 ng-model。在这种情况下,我们永远无法确定在方法 userName 中引用 WebElement 时是否会强制引用not NULL 值。它也可能是NULL

现在让我们看一下visibilityOf方法定义,它的定义为:

visibilityOf(WebElement element)
An expectation for checking that an element, known to be present on the DOM of a page, is visible.

很明显,visibilityOf方法需要WebElement直接引用。因此,在 userName 缺乏确定值(not NULL)的情况下,WebDriverWait这是 FluentWait 的变体,显示错误。

关于java - wait.until(ExpectedConditions.visibilityOf(userName)) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47656980/

相关文章:

java - 如何取消代理 hibernate 对象

java - Selenium:帮助验证元素是否按价格排序

java - 为什么下面的Java Stream filter方法没有抛出NullPointerException

java - 将 ALTO XML 转换为格式化的 PDF/RTF/TXT?

java.lang.OutOfMemory错误: unable to create new native thread executing Selenium WebDriver based tests using Selenium and Java

Selenium 独立服务器未启动

java - Selenium driver.getPageSource() ,将链接更改为绝对

java - 为什么这会返回 "org.apache.derby.client.net.NetResultSet42@1b5f7aa"

python-3.x - 如何使用selenium python自动扫描二维码

python - 如何为 CEF 应用程序编写 Selenium 测试