Python中的Selenium:选择一个选项 [英] Selenium in Python: Selecting an Option

查看:67
本文介绍了Python中的Selenium:选择一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有运气用硒从< select> 中选择一个选项.我已引用 https://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver

I am having no luck selecting an option from a <select> using selenium. I have referenced https://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver

html代码如下:

<select name="Dropdownlistrequests" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;Dropdownlistrequests\&#39;,\&#39;\&#39;)&#39;, 0)" id="Dropdownlistrequests" style="height:51px;width:174px;Z-INDEX: 104; LEFT: 280px; POSITION: absolute; TOP: 72px">
    <option selected="selected" value="1">Previous Days</option>
    <option value="2">Previous Month</option>
    <option value="3">Last 12 Hours</option>
    <option value="4">Demand Poll</option>
    <option value="6">Custom</option>
</select>

我尝试过

requests = driver.find_element_by_id("Dropdownlistrequests")
requests.click()
for option in requests.find_elements_by_tag_name('option'):
    if option.text == "Custom":
        option.click()
        break

还有

requests = Select(driver.find_element_by_id("Dropdownlistrequests"))
requests.select_by_value("6")

还有

b.find_element_by_xpath("//select[@id='Dropdownlistrequests']/option[text()='Custom']").click()

浏览器没有选择适当的选项,而是不执行任何操作,而是继续执行下一部分代码.它可以做些由onchange触发的javascript吗?

Instead of selecting the appropriate option the browser does nothing and moves on to the next bit of code. Could it have something to do the the javascript that is triggered by the onchange?

要提供更多背景信息:我正在运行Windows 7企业版,并且将硒与木偶和Firefox开发人员版本49.0a2配合使用

To provide some more context: I am running windows 7 enterprise and using selenium with marionette and Firefox developer edition 49.0a2

更新:只有在python中使用木偶时才会发生这种情况.我在带有和不带有Marionette的Java中尝试了相同的代码,并且有效

Update: This only sems to happen when usoing Marionette in python. I tried this same code in Java with and without Marionette and it worked

推荐答案

如果您的工作不可行,则应尝试使用 .execute_script(),如下所示:-

If non of the work in your case you should try .execute_script() as below :-

select = driver.find_element_by_id("Dropdownlistrequests")

driver.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }",select, "Custom")

已编辑:-上面的代码仅从选择框中选择提供的选项.如果在选择选项时也要触发 onchange 事件,请尝试以下操作:-

Edited :- Above code only select the provided option from select box. If you want trigger the onchange event as well when option selected, try as below :-

select = driver.find_element_by_id("Dropdownlistrequests")

driver.execute_script("showDropdown = function (element) {var event; event = document.createEvent('MouseEvents'); event.initMouseEvent('mousedown', true, true, window); element.dispatchEvent(event); }; showDropdown(arguments[0]);",select)
# now you dropdown will be open


driver.find_element_by_xpath("//select[@id='Dropdownlistrequests']/option[text()='Custom']").click()
#this will click the option which text is custom and onchange event will be triggered.

希望它对您有用..:)

Hope it will work for you..:)

这篇关于Python中的Selenium:选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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