硒为wait.until抛出空指针

时间:2019-09-24 03:31:17

标签: selenium selenium-webdriver salesforce salesforce-service-cloud

尝试自动化Salesforce中的页面,在尝试等待元素时看到奇怪的问题。

@FindBy(xpath = "//span[@title='console']")
private WebElement consoleTitle;

public void switchApplicationLightening(String applicationName) throws InterruptedException {
    String st = util.driver.getPageSource(); //This step to debug I am seeing null here
    String str = util.driver.findElement(By.xpath("//span[@title='console']")).getText(); // This step is not required but added to debug and this is working fine
     if(!verifyElementVisible(consoleTitle, 5)){ //Its failing here and seeing issue
        switchToApplication(applicationName);
    }
}

public static Boolean verifyElementVisible(WebElement element, int explicitWait) {
    WebDriverWait wait = new WebDriverWait(util.driver, explicitWait);
    System.out.println(util.driver);
    try {
        wait.until(ExpectedConditions.visibilityOf(element));
        return true;
    } catch (NoSuchElementException | NoSuchFrameException | NoSuchWindowException | ErrorHandler.UnknownServerException | TimeoutException e) {
        VERIFICATION_ERRORS.append("Element: ").append(element).append(" is not present on page \n -Caugth exception: ").append(e.getMessage()).append("\n\n");
        return false;
    }
}

在步骤-wait.until(ExpectedConditions.visibilityOf(element));上看到以下错误

java.lang.NullPointerException
    at org.openqa.selenium.remote.RemoteWebElement.isDisplayed(RemoteWebElement.java:320)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:51)
    at com.sun.proxy.$Proxy31.isDisplayed(Unknown Source)
    at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:315)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$100(ExpectedConditions.java:44)
    at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:301)
    at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:298)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:248)

似乎不喜欢wait.until-每次被称为它的抛出空指针

PS:请忽略字符串str = util.driver.findElement(By.xpath(“ // span [@ title ='console']”))。getText();我在等待以下相同元素的情况下将其用于调试不合逻辑的操作。同样,getPageSource()为null,但我执行了下一步。

3 个答案:

答案 0 :(得分:0)

我认为从代码元素作为null传递给此方法,请检查您如何调用此方法。 我看到您有consoleTitle,但是您从未为该变量分配任何值。我认为您应该分配consoleTitle = str或改用str。

答案 1 :(得分:0)

如果使用@FindBy,则必须使用PageFactory.initElements()初始化webElement。

public void switchApplicationLightening(String applicationName) throws InterruptedException {
    PageFactory.initElements(util.driver, this);
    String st = util.driver.getPageSource(); //This step to debug I am seeing null here
    String str = util.driver.findElement(By.xpath("//span[@title='console']")).getText(); // This step is not required but added to debug and this is working fine
     if(!verifyElementVisible(consoleTitle, 5)){ //Its failing here and seeing issue
        switchToApplication(applicationName);
    }
}

正在导入:

import org.openqa.selenium.support.PageFactory;

答案 2 :(得分:0)

我有同样的问题。在闲缺的频道上与硒开发人员一起使用。当我升级到chrome 78和最新的chromedriver时,此问题已解决。等待再次与PageFactory一起使用。铬77中存在一个导致NPE的严重问题。