Dropdown not getting selected using Selenium Webdriver on Safari browser 10 in Mac
Asked Answered
M

5

6

On Safari browser I need to select an Option from the dropdown. below code works for all the browsers except for Safari on Mac OS. I am using Safari 10.1.1 with selenium web driver version 3.3.1 I have written the code in Java. Refer the code below -

webElement = findElement(field);
if (webElement.isDisplayed())
{
  Select select = new Select(webElement);
  select.selectByVisibleText(value);
}
Mariannmarianna answered 30/5, 2017 at 11:38 Comment(7)
are you getting any err message?Snelling
No, I did not receive any error message.Mariannmarianna
Can you give me the DOM?Snelling
I am facing this issue in Safari browser only. and unable to maximize the Safari windowMariannmarianna
I am not able to guess the issue... Can you give me more details like site you are trying with so that i can try with my mac and help?Snelling
Dom details <select id="profileItem_10536" name="value(profileItem_10536)" onchange="change_profileItem_10536()" required="required"> <option id="profileItem_10536_select_one" value="">-- select one --</option> <option id="profileItem_10536_11703" value="11703">Yes</option> <option id="profileItem_10536_11704" value="11704">No</option> <option id="profileItem_10536_12292" value="12292">Already Booked</option>Mariannmarianna
site sharing is not possibleMariannmarianna
R
2

You can try this code:

public void jsSelect(WebElement element, int index) {
        JavascriptExecutor executor = (JavascriptExecutor) driver;
        executor.executeScript("arguments[0].selectedIndex=" + index + ";", element);
    }

public void jsSelect(WebElement element, String item) {
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("const textToFind = '" + item + "';" +
            "const dd = arguments[0];" +
            "dd.selectedIndex = [...dd.options].findIndex (option => option.text === textToFind);", element);
}
Ramulose answered 10/3, 2018 at 14:14 Comment(2)
You could explain what you changed.Forwarder
With these methods you can select by index or visible text the value in select-option element using JavaScript, this works for me with safari driverRamulose
S
1

Can you check, if the below code works in Safari..

WebElement dropdown = driver.findElement(By.xpath("//select[@id='profileItem_10536']"));
Select sel = new Select(dropdown);
sel.selectByVisibleText("Yes");

If the code doesn't work in Safari and works in other browsers let me know...

Update:

Everything should work fine on Sierra if you use the correct driver (supplied by Apple). You shouldn't be using Selenium's SafariDriver with Safari 10.

specifically:

"The old SafariDriver implementation is no longer maintained and should not be used." "Safari now provides native support for the WebDriver API. Starting with Safari 10 on OS X El Capitan and macOS Sierra, Safari comes bundled with a new driver implementation that’s maintained by the Web Developer Experience team at Apple." Also note:

"Safari’s WebDriver support is turned off by default"

It seems apple provided its own safari driver and its available at

"/usr/bin/safaridriver"

Kindly use this drive. For more details regarding this kindly look into https://webkit.org/blog/6900/webdriver-support-in-safari-10/ and https://github.com/SeleniumHQ/selenium/issues/3145

Hope this helps you. Thanks.

Snelling answered 1/6, 2017 at 5:3 Comment(4)
Thank you for your response. above code is working in Chrome browser not working in Safari browserMariannmarianna
Last two days onwards I am facing this issue. before that same code working in Safari browser also. i am unable to find out what is the issueMariannmarianna
In this case, can you check updating safari driverSnelling
In Safari >> Preferences>>Extensionlist added webdriver 2.45Mariannmarianna
I
0

Had the same problem on Java Selenium 3.141.59 when using the default Select that is npt supported by Safari webdriver, tried also with clicks on the Select web element and then on the targeted option...also not working, resolved it by using Javascript to select the desired option and then fire the relevant event in order to simulate the onchange action :

public void selectFromDropDownForSafari(final WebDriver driver, final String selectId, final String expectedOption) {
    WebElement selectElement =  driver.findElement(By.id(selectId)); 
    // Get the option tags under the Select element
    List<WebElement> options = selectElement.findElements(By.tagName("option"));
    if(CollectionUtils.isNotEmpty(options)) {
        for(int index = 0; index < options.size(); index++) {
            WebElement option = options.get(index);
            // Search for an option having the same value attribute OR the displayed text (handling both possibilities)
            if(expectedOption.equals(option.getAttribute("value")) || expectedOption.equals(option.getText())) {
                JavascriptExecutor executor = (JavascriptExecutor) driver;
                // Select the targeted option by index and launch the onchange event
                executor.executeScript("arguments[0].selectedIndex=" + index + "; arguments[0].dispatchEvent(new Event('change'));", selectElement);
                return;
            }
        }
        throw new Exception("No option with the desired value [" + expectedOption + "] was found in the Select element");
    }
    throw new Exception("The Select element does not have any option");
}

On my case, this wouldn't work properly without sending the corresponding event by javascript (dispatchEvent)

Impropriate answered 25/5, 2023 at 9:34 Comment(0)
H
-1

Select is not working on Sierra 10,12.6 safari 11.0 I tried the below and they do not work as nothing happen and no error shown.

selectByIndex(3) 
selectByValue("value"), 
selectByVisibleText("Yes");

If you try to use : Send key method then the select option is populated.

Hydrophilic answered 24/10, 2017 at 13:55 Comment(1)
This does not answer the question.Supinate
S
-1

None of the dropdown select functions work in Safari. As a workaround I have used element.type("") in Selenium and it worked in place of Select.

Succulent answered 21/11, 2017 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.