Java – the drop-down list cannot be selected using selenium webdriver on safari browser 10 on Mac

In Safari browser, I need to select an option from the drop-down list

webElement = findElement(field);
if (webElement.isDisplayed())
{
  Select select = new Select(webElement);
  select.selectByVisibleText(value);
}

Solution

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);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>