在Mac上的Safari浏览器10上使用Selenium Webdriver未选择下拉列表

时间:2017-05-30 11:38:52

标签: java selenium-webdriver

在Safari浏览器上,我需要从下拉列表中选择一个选项。 以下代码适用于除Mac OS上的Safari之外的所有浏览器。我使用的是Safari 10.1.1和selenium web驱动程序版本3.3.1我用Java编写了代码。请参阅以下代码 -

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

4 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

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);
}

答案 1 :(得分:0)

如果下面的代码在Safari中有效,你能检查一下吗?

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

如果代码在Safari中不起作用并且在其他浏览器中有效,请告诉我......

<强>更新

如果使用正确的驱动程序(由Apple提供),Sierra上的所有内容都可以正常工作。你不应该在Safari 10中使用Selenium的SafariDriver。

  

具体是:

     

&#34;旧的SafariDriver实现不再维护了   不应该使用。&#34; &#34; Safari现在提供原生支持   WebDriver API。从OS X El Capitan和macOS上的Safari 10开始   Sierra,Safari捆绑了一个新的驱动程序实现   由Apple的Web开发人员体验团队维护。&#34;另请注意:

     

&#34;默认情况下,Safari的WebDriver支持已关闭&#34;

似乎苹果提供了自己的野生动物园驱动程序,并在

提供
  

&#34;在/ usr /斌/ safaridriver&#34;

请使用此驱动器。有关此问题的详细信息,请查看https://webkit.org/blog/6900/webdriver-support-in-safari-10/https://github.com/SeleniumHQ/selenium/issues/3145

希望这会对你有所帮助。感谢。

答案 2 :(得分:0)

选择不适用于Sierra 10,12.6 safari 11.0 我尝试了下面的内容,它们不起作用,因为没有任何事情发生,也没有显示错误。

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

如果您尝试使用: 发送密钥方法然后填充选择选项。

答案 3 :(得分:-1)

所有下拉选择功能都不适用于Safari。 作为一种解决方法,我在Selenium中使用了element.type(“”),它代替了Select。