2

Following line gives error:

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

Code is as below:

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')]")));**

Also, I have observed:

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

is working fine.

How to handle it?

Stack Trace:

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)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Neha
  • 21
  • 5

3 Answers3

0

I noticed you have implicitlyWait declared in your WebDriver. When you have implicitlyWait configured, you don't need to use explicit waits such as wait.until.

When you mix implicit and explicit waits like in this example, you may get some unexpected results, some explanation here.

You can try to remove wait.until statements and see if your code still works.

CEH
  • 5,701
  • 2
  • 16
  • 40
0

the difference is that visibilityOfElementLocated() checks some element styles like width and height, while presenceOfElementLocated() should check only existence. https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#visibilityOfElementLocated-org.openqa.selenium.By-

Is your element really visible on the page?


Also it would be nice to see the stacktrace, as .until() should raise TimeoutException if element is not found, not Null Pointer

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

lGSMl
  • 151
  • 1
  • 11
  • 1
    Element is visible on the webpage. Stack trace is as follows – Neha Sep 26 '19 at 16:48
  • what version of the selenium are you using please? There was an issue with this indeed that was fixed a year ago https://github.com/clarmso/selenium/commit/41474ee1a4718aeb0d3ab53f5ad5d4e7645105ea https://github.com/SeleniumHQ/selenium/issues/5809 – lGSMl Sep 30 '19 at 11:13
  • and yeah, fix should be available since selenium-3.141.0 – lGSMl Sep 30 '19 at 11:25
0

WebDriverManager

WebDriverManager allows to automate the management of the binary drivers (e.g. chromedriver, geckodriver, etc.) required by Selenium WebDriver. By adding the following line:

WebDriverManager.chromedriver().setup(); 

WebDriverManager does the following tasks for you:

  • It checks the version of the browser installed in your machine (i.e. Chrome).
  • It checks the version of the driver (i.e. chromedriver). If unknown, it uses the latest version of the driver.
  • It downloads the WebDriver binary if it is not present on the WebDriverManager cache (~/.m2/repository/webdriver by default).
  • It exports the proper WebDriver Java environment variables required by Selenium.

Moving forward instead of creating the driver from the ChromeDriver Class you need to use the WebDriver interface. So you need to replace:

ChromeDriver driver = new ChromeDriver(options);

with:

WebDriver driver = new ChromeDriver(options);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352