2

以下行给出错误:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='username']")));

代码如下:

WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--disable-notifications");
ChromeDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.get("https://test.salesforce.com/");
WebDriverWait wait=new WebDriverWait(driver, 120);
driver.findElement(By.id("username")).sendKeys("");
driver.findElement(By.id("password")).sendKeys("");
driver.findElement(By.id("Login")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@class,'home-accounts')]")));**

另外,我观察到:

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[contains(@class,'home-accounts')]")));

工作正常。

如何处理?

堆栈跟踪:

java.lang.NullPointerException
    at org.openqa.selenium.remote.RemoteWebElement.isDisplayed(RemoteWebElement.java:323)
    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$7.apply(ExpectedConditions.java:206)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:202)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:248)
    at TestClass.NewTest.TC_DealDetails_0(NewTest.java:70)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
4

3 回答 3

0

网络驱动管理器

WebDriverManager允许自动管理Selenium WebDriver所需的二进制驱动程序(例如 chromedriver、geckodriver 等)。通过添加以下行:

WebDriverManager.chromedriver().setup(); 

WebDriverManager为您执行以下任务:

  • 它会检查您机器中安装的浏览器版本(即 Chrome)。
  • 它检查驱动程序的版本(即 chromedriver)。如果未知,它将使用最新版本的驱动程序。
  • 如果 WebDriverManager 缓存中不存在 WebDriver 二进制文件,它会下载它(~/.m2/repository/webdriver默认情况下)。
  • 它导出 Selenium 所需的正确 WebDriver Java 环境变量。

继续前进,而不是driverChromeDriver 创建您需要使用WebDriver 接口。所以你需要更换:

ChromeDriver driver = new ChromeDriver(options);

和:

WebDriver driver = new ChromeDriver(options);
于 2019-09-26T21:28:41.220 回答
0

我注意到你已经implicitlyWait在你的 WebDriver 中声明了。配置完成后,您implicitlyWait不需要使用显式等待,例如wait.until.

当你像这个例子一样混合隐式和显式等待时,你可能会得到一些意想不到的结果,这里有一些解释。

您可以尝试删除wait.until语句,看看您的代码是否仍然有效。

于 2019-09-26T15:59:27.713 回答
0

不同之处在于visibilityOfElementLocated()检查一些元素样式,如宽度和高度,而presentOfElementLocated()应该只检查存在。 https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#visibilityOfElementLocated-org.openqa.selenium.By-

你的元素在页面上真的可见吗?


也很高兴看到堆栈跟踪,因为如果找不到元素,.until() 应该引发 TimeoutException,而不是空指针

https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

于 2019-09-26T16:10:59.670 回答