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

时间:2017-08-31 10:13:10

标签: javascript ruby selenium-webdriver watir microsoft-edge

我的应用程序有一项要求是从microsoft edge browser中的列表框中选择多个项目

我正在使用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>

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

@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使用 execute_script 在Microsoft边缘浏览器中选择多个选项。

1 个答案:

答案 0 :(得分:1)

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

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

在此之前,您需要执行JavaScript来选择选项。

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

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"))