【问题标题】:select multiple options in listbox edge browser ruby selenium webdriver在列表框边缘浏览器中选择多个选项 ruby​​ selenium webdriver
【发布时间】:2017-08-31 10:13:10
【问题描述】:

我的应用程序需要从 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>

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

@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 但它没有帮助。有没有其他方法可以使用 execute_script 使用 javascript 在 Microsoft Edge 浏览器中选择多个选项。

【问题讨论】:

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


    【解决方案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"))
    

    【讨论】:

    • 感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 2015-11-16
    • 2023-04-11
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    相关资源
    最近更新 更多