Search code examples
selenium-webdriversafaridriver

Selenium code is not working for locating an element in Safari, working in Firefox and Chrome


Can someone help who has worked on Selenium WebDriver?

I have been trying to automate a test scenario using Selenium WebDriver on a Mac machine. When I define Safari as my browser, I am getting and error "An element could not be located on the page using the given search parameters", even though that elements exists on the page in java code Issues/Bug.

Note: the same element can be located when we choose Firefox and Chrome as browser. There are some similar answers, but none of them is talking about Safari browser and Mac machine.


Solution

  • Try to put some wait. Use fluent wait as below :-

    WebElement waitsss(WebDriver driver, By elementIdentifier){
         Wait<WebDriver> wait =
                    new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS)
                                                     .pollingEvery(1, TimeUnit.SECONDS)
                                                     .ignoring(NoSuchElementException.class);
    
        return wait.until(new Function<WebDriver, WebElement>()
                {
                    public WebElement apply(WebDriver driver) {
                           return driver.findElement(elementIdentifier);
                    }
                    });
    }
    

    The wait should work for you. If still the problem exists then use JavascriptExecutor . It will operate directly through JS. It should work. I am giving an example to click any element using JavascriptExecutor

    WebElement element = driver.findElement(By.id("gbqfd"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", element);
    

    Hope it will help you :)