使用JavaScript交互下拉使用的webdriver和Ruby菜单? [英] Interacting with JavaScript drop down menu using WebDriver and Ruby?

查看:147
本文介绍了使用JavaScript交互下拉使用的webdriver和Ruby菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写使用Ruby和硒的webdriver网页自动化,但也遇到阻塞问题与其中一些正在使用,而不是标准的JavaScript的HTML制作网站上的下拉菜单中。这些下拉菜单用户可以从下拉选项关闭或点击它,并开始输入为它基于什么键入筛选结果(我假设的想法是这些下拉菜单是潜在的非常长的列表选项​​)。

问题是我无法想出一个办法与他们使用webdriver的互动,虽然我设法得到它使用低于code在硒IDE工作,但出口这个IDE脚本红宝石没有按' ŧ工作:

 < TR>
    < TD>选择< / TD>
    < TD>的CSS =选择[名称= dsref] LT; / TD>
    < TD>示范及LT; / TD>
< / TR>

该webdriver的code,我现在用的是:

 高清clickOn(参数= {})
      #这将接受一个定位器类型和名称点击
      #
      #示例
      #
      # 点击(
      #locatorType:链接,
      #locatorName:家
      #)
      #
      #这会点击一个名为主页的链接
      locatorType =参数[:locatorType]
      locatorName =参数[:locatorName]
      $ driver.find_element(locatorType,locatorName)。点击
    结束    点击(
          locatorType:XPath中,
          locatorName:'/ HTML /体/表格/字段集[4] / DIV [2] / DIV / DIV / DIV / A /跨度
      )

我已然后试图从下拉在使用隐藏值字段的xpath的相同的方式定位的选择,但它是不能够找到隐藏值

这是我想与之交互的JavaScript是:

 < D​​IV ID =tabWarehousing级=能够看见的>
< D​​IV CLASS =lineFirst>
< D​​IV CLASS =8,6,4,2,0,1>
    <标签=formdsref>仓储LT; /标签>
    < BR>
    <选择一个id =sel2DZ级=admn_dropdownmenu chzn-做大小=1NAME =dsref的风格=显示:无;>
        选择= {获选= form_field选择#sel2DZ.admn_dropdownmenu,选择= {...},active_field =        真正        , 更多...}        <选项选择=值=>< /选项>
        <期权价值=演示>示范及LT; /选项>
        <期权价值=demodownload>下载演示< /选项>
    < /选择>
    < D​​IV ID =sel2DZ_chzn级=chzn容器chzn容器单chzn容器,单nosearch的风格=WIDTH:115px;>
        <一类=chzn单的tabindex = - 1HREF =JavaScript的:无效(0)> <跨度>示范及LT; / SPAN>
        < D​​IV>
            < B>< / B>
        < / DIV>
        &所述; / A>
        < D​​IV CLASS =chzn降的风格=左:-9000px; WIDTH:113px;顶部:18像素;>
            < D​​IV CLASS =chzn搜索>
                <输入类型=文字自动完成=关闭的风格=宽度:78px;>
            < / DIV>
            < UL类=chzn-结果>
                <李ID =sel2DZ_chzn_o_1级=有源结果结果选择的风格=>示范及LT; /李>
                <李ID =sel2DZ_chzn_o_2级=有效结果的风格=>下载演示< /李>
            < / UL>
        < / DIV>
    < / DIV>
< / DIV>

我如何可以设置此下拉利用价值的任何想法红宝石的webdriver?


解决方案

JavaScript的只是一个隐藏的下拉互动,这样你就可以取消隐藏实际的下拉使用:

<$p$p><$c$c>@driver.execute_script(\"document.getElementsByName('#{<REF_TO_HIDDEN_DROPDOWN}')[0].style.display ='块')

这将隐藏下拉菜单的显示设置为'块',因此您可以正常与它进行交互。

I'm writing automation for a web page using Ruby and Selenium WebDriver but have encountered a blocking issue with some of the drop downs on the site which are made using JavaScript instead of standard HTML. On these drop downs users are able to select an option from the drop down or click on it and start typing for it to filter results based on what is typed (I'm assuming the idea is these drop downs are for potentially very long lists of options).

Problem is I can't figure out a way to interact with them using WebDriver, though i have managed to get it to work in the Selenium IDE using the below code, but exporting this IDE script to Ruby doesn't work:

<tr>
    <td>select</td>
    <td>css=select[name=dsref]</td>
    <td>demo</td>
</tr>

The WebDriver code that I am using is:

def clickOn (parameters = {})
      # This will accept a locator type and name to click on
      #
      # EXAMPLE
      #
      # clickOn(
      #   locatorType: :link,
      #   locatorName: 'Home'
      # )
      #
      # This will click on the link named 'Home'
      locatorType = parameters[:locatorType]
      locatorName = parameters[:locatorName]
      $driver.find_element(locatorType, locatorName).click
    end

    clickOn(
          locatorType: :xpath,
          locatorName:     '/html/body/form/fieldset[4]/div[2]/div/div/div/a/span'
      )

I have then tried to locate the selection from the drop down in the same way using the xpath of the hidden values field, but it is not able to locate the hidden values.

The javascript that I am trying to interact with is:

<div id="tabWarehousing" class="cansee">
<div class="lineFirst">
<div class="of8">
    <label for="formdsref">Warehouse</label>
    <br>
    <select id="sel2DZ" class="admn_dropdownmenu chzn-done" size="1" name="dsref"     style="display: none;">
        chosen=Chosen { form_field=select#sel2DZ.admn_dropdownmenu, options= {...}, active_field=

        true

        , more...}

        <option selected="" value=""></option>
        <option value="demo">demo</option>
        <option value="demodownload">demo download</option>
    </select>
    <div id="sel2DZ_chzn" class="chzn-container chzn-container-single chzn-container-single-  nosearch" style="width: 115px;">
        <a class="chzn-single" tabindex="-1" href="javascript:void(0)"> <span>demo</span>
        <div>
            <b></b>
        </div>
        </a>
        <div class="chzn-drop" style="left: -9000px; width: 113px; top: 18px;">
            <div class="chzn-search">
                <input type="text" autocomplete="off" style="width: 78px;">
            </div>
            <ul class="chzn-results">
                <li id="sel2DZ_chzn_o_1" class="active-result result-selected" style="">demo</li>
                <li id="sel2DZ_chzn_o_2" class="active-result" style="">demo download</li>
            </ul>
        </div>
    </div>
</div>

Any ideas on how I can set the value of this drop down using Ruby WebDriver?

解决方案

The javascript is just interacting with a hidden dropdown, so you can unhide the actual drop down using:

@driver.execute_script("document.getElementsByName('#{<REF_TO_HIDDEN_DROPDOWN}')[0].style.display = 'block'")

This will set the display of the hidden dropdown to 'block' so you can interact with it as normal.

这篇关于使用JavaScript交互下拉使用的webdriver和Ruby菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆