【问题标题】:Selenium throwing null pointer for wait.untilSelenium 为 wait.until 抛出空指针
【发布时间】:2020-01-24 03:07:09
【问题描述】:

尝试在 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 - 每次调用 this 时都会抛出空指针

PS: 请忽略 String str = util.driver.findElement(By.xpath("//span[@title='console']")).getText();这是我为调试不合逻辑的,因为我们正在等待下面的相同元素。 getPageSource() 也为空,但我执行下一步。

【问题讨论】:

  • 进一步调试我发现 wait.until 正在等待如果页面上不存在元素并抛出如下错误:org.openqa.selenium.TimeoutException:预期条件失败:等待代理元素的可见性对于: DefaultElementLocator 'By.xpath: //span[@title='con']' (尝试 30 秒,间隔 500 毫秒)在上面我故意给 xpath 错误。当 xpath 正确时,我看到空指针异常。
  • 你初始化页面工厂了吗?
  • 是的,我确实初始化了页面工厂。进一步调试我发现问题出在 ExpectedConditions.visibilityOf(element) 上,而不是如果我使用 enExpectedConditions.presenceOfElementLocated 它的工作。所以可见性存在一些问题
  • 存在和可见性是两个不同的东西

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


【解决方案1】:

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

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;

【讨论】:

  • OP在评论中说他初始化了Page Factory
  • 感谢确认
【解决方案2】:

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

【讨论】:

  • 我打印了元素并看到了这个结果:[[ChromeDriver: chrome on MAC (3d78cf21f32f647c41339ec65d2fd92f)] -> xpath: //span[@title='8x8 Service Console']] 所以元素没有问题. consoleTitle 具有 xpath 值。我不明白为什么我必须将 str 分配给它?
  • 我在上面使用 str 或 st 只是为了调试并查看值是什么——这两行对我没有用。
【解决方案3】:

我也有同样的问题。在他们的 slack 频道上与 selenium 开发人员进行了协商。当我升级到 chrome 78 和最新的 chromedriver 时,它得到了解决。等待再次与 PageFactory 一起工作。 chrome 77 中存在一个导致 NPE 的关键问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多