【发布时间】: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