1

It appears that the SafariDriver for Selenium doesn't wait for web pages to load. My test is as follows:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.safari.SafariDriver;

public class SafariTest {

    private WebDriver driver;

    @Before
    public void setUp() throws Exception {
        driver = new SafariDriver();
    }

    @After
    public void tearDown() throws Exception {
        driver.close();
    }

    @Test
    public void testGoogleSearch() {
        driver.get("http://duckduckgo.com");
        driver.findElement(By.id("search_form_input_homepage")).sendKeys("Hello World");
        driver.findElement(By.id("search_button_homepage")).click();
        driver.findElement(By.linkText("Images")).click();
    }
}

If you run this with ChromeDriver, or FirefoxDriver, it functions as it should, i.e. it searches for "Hello World", then on the results page, it goes to the image results.

With SafariDriver, it fails with:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

The element that could not be found being "Images", since the page hasn't loaded before it ran that statement.

Is this expected behavior? Am I supposed to special case for Safari?

1

You have to account for that with an Explicit Wait with WebDriverWait:

WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);

WebElement images = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Images")));
images.click();
  • 1
    Why would I have to do this only with Safari and no other browser? – Roxy Aug 3 '17 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.