【问题标题】:Choose dropdown list item in selenium webdriver using javascript使用 javascript 在 selenium webdriver 中选择下拉列表项
【发布时间】:2022-01-27 10:54:24
【问题描述】:

我一直在努力使用javascript 在 selenium webdriver 中选择下拉列表项。 我的任务是打开 https://pastebin.com/ 然后使用 selenium webdriver javascript 从下拉列表中选择 "10 Minutes"。 我已经实现了通过以下方法打开下拉列表:

await driver.findElement(By.id(`postform-expiration`)).click()

尝试同时选择下拉列表项 li 值和 id:

await dropdown.findElement(By.css(`li[value="10 Minutes"]`)).click()
// and
await driver.findElement(By.css(`//*[@id="select2-postform-expiration-result-b5nq-10M"]`)).click()

但是他们都不能选择下拉列表项。

下拉列表的结构如下:

<div class="col-sm-9 field-wrapper">
<select id="postform-expiration" class="form-control select2-hidden-accessible" name="PostForm[expiration]" data-s2-options="s2options_7ebc6538" data-krajee-select2="select2_a09a7382" style="width: 1px; height: 1px; visibility: hidden;" data-select2-id="postform-expiration" tabindex="-1" aria-hidden="true">
<option value="N" data-select2-id="4">Never</option>
<option value="B" data-select2-id="11">Burn after read</option>
<option value="10M" data-select2-id="12">10 Minutes</option>
<option value="1H" data-select2-id="13">1 Hour</option>
<option value="1D" data-select2-id="14">1 Day</option>
<option value="1W" data-select2-id="15">1 Week</option>
<option value="2W" data-select2-id="16">2 Weeks</option>
<option value="1M" data-select2-id="17">1 Month</option>
<option value="6M" data-select2-id="18">6 Months</option>
<option value="1Y" data-select2-id="19">1 Year</option>
</select><span class="select2 select2-container select2-container--default select2-container--open select2-container--above select2-container--focus" dir="ltr" data-select2-id="3" style="width: 100%;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="true" tabindex="0" aria-disabled="false" aria-labelledby="select2-postform-expiration-container" aria-owns="select2-postform-expiration-results" aria-activedescendant="select2-postform-expiration-result-e4wf-1D"><span class="select2-selection__rendered" id="select2-postform-expiration-container" role="textbox" aria-readonly="true" title="Never">Never</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>

</div>

感谢您的帮助。提前致谢!

【问题讨论】:

    标签: javascript selenium selenium-webdriver webdriver


    【解决方案1】:

    您为什么不尝试选择方法。它应该适用于那个特定的下拉菜单。如下:

    Select selectTime = new Select(driver.findElement(By.className("select2-selection select2-selection--single")));
    selectTime.selectByValue("10 Minutes");
    

    另外,如果这不起作用,请尝试类似的方法(这与您的方法更相似):

    await dropdown.findElement(By.id(`select2-postform-expiration-container`)).click()
    await driver.findElement(By.xpath(`//*[text("10 Minutes"))).click();
    
    

    【讨论】:

    • Select 也可以在 js 中工作吗?我应该在使用前要求它吗?
    • 我不确定。你可以试试看:)。我忘了你是用JS写的。我的建议是,对于自动化,使用 Java 或 Python 编写。 JScript 是一种脚本语言,与任何脚本语言一样,它的变量是 OLE 兼容的,这意味着实际类型是无关紧要的(在大多数情况下)并且由脚本运行时引擎在内部处理。
    • 我以前试过。但是在 selenium-webdriver for js 中没有 Select 类或接口。我一直在考虑改用 Java。
    • 是的,请切换到 Java。也会轻松很多!然后使用我在答案中写的选择方法。
    猜你喜欢
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多