select 列表框边缘浏览器中的多个选项 ruby selenium webdriver

select multiple options in listbox edge browser ruby selenium webdriver

我的应用程序需要 select Microsoft Edge 浏览器列表框中的多个项目

我正在使用 watir webdriver 来测试我的应用程序

DOM结构如下:

<div id="textSearch">
<div id="textSearch">
<select name="@Type" id="textType" onchange="unselectOptionZero('@Type');" size="7" multiple="" width="250">
<option value="*" selected="">- All -</option>
<option value="text1">text1</option>
<option value="text2">text2</option>
<option value="text3">text3</option>
<option value="text4">text4</option>
<option value="text5">text5</option>
</select>
</div>
</div>

我尝试了以下命令来 select 多个值

@browser.select_list(:id, "textType").option(:value => "text3").select
@browser.send_keys :control
@browser.select_list(:id, "textType").option(:value => "text4").select

这似乎不起作用。我尝试通过 .select 使用迭代,但它似乎不起作用。

我也试过 selenium 支持 Selenium::WebDriver::Support::Select.new 但它没有帮助。使用 javascript.

在 Microsoft Edge 浏览器中使用 execute_script 有没有其他方法 select 多个选项

Watir 的 Select#select select 通过调用 #click 方法来设置选项。与其他驱动程序不同,Edge 将此视为常规点击,取消了select之前的选项。这是 Microsoft Edge 团队的 known/expected behaviour

他们的建议是使用 Actions 对象来按住控制按钮。但是,尝试这样做,例如通过调用 option.click(:control),将导致未知的命令异常。 Edge 驱动程序有 not yet implemented the Actions command.

在那之前,您需要执行 JavaScript 到 select 选项。

如果您使用的是 Watir v6.8 或更高版本,您可以使用新的 #select! 方法通过 JavaScript 而不是鼠标点击来 select 选项。这将保留之前 selected 的值。

s= @browser.select_list(:id, "textType")
s.select!("text3")
s.select!("text4")

请注意,#select 现在支持通过文本和值查找选项(与仅检查文本的先前版本相反)。

如果您使用的是早期版本的 Watir,同样可以使用 #execute_script:

s= @browser.select_list(:id, "textType")
select_script = 'arguments[0].selected=true;'
@browser.execute_script(select_script, s.option(:value => "text3"))
@browser.execute_script(select_script, s.option(:value => "text4"))