如何从硒中选择自定义下拉列表元素 [英] how to select custom dropdown list element from selenium

查看:253
本文介绍了如何从硒中选择自定义下拉列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从硒中选择自定义下拉列表元素。

how to select custom dropdown list element from selenium.

我想从下拉列表中选择一个国家,使用selenium python webdrive。

I want to select a country from the dropdown list, using selenium python webdrive.

<select id="id_country" class="hidden-field" name="country" data-id="1394114974464-fOg4n">
<div class="custom dropdown" data-id="1394114974464-fOg4n">
<a class="current" href="#">Belize</a>
<a class="selector" href="#"></a>
<ul>
<li class="">Select Your Country</li>
<li class="">Afghanistan</li>
<li>Albania</li>
<li class="">Algeria</li>
<li class="">American Samoa</li>
<li class="">Andorra</li>
<li>Angola</li>
<li class="">Anguilla</li>
<li class="">Antigua and Barbuda</li>
<li class="">Argentina</li>
<li class="">Armenia</li>


推荐答案

当前推荐的方法从下拉菜单中选择一个项目是使用Select类。以下是一个简单的例子:

The currently recommended method of selecting an item from a dropdown menu is to use the Select class. Here's a quick example:

from selenium.webdriver.support.ui import Select
from selenium import webdriver

browser = webdriver.Firefox()
browser.get("file:///C:\testing\\test.html")

element = browser.find_element_by_id("id_country")
select = Select(element)
select.select_by_visible_text("Armenia")

但是,您发布的HTML似乎不起作用;我只需要一个空的下拉框。您需要修复它才能使用上述代码。例如:

However, the HTML you posted doesn't seem to work; I just get an empty dropdown box. You'll need to fix that in order to be able to use the above code. For example:

<html><body>
<select id="id_country" name="country">
<option>Select Your Country</option>
<option>Afghanistan</option>
<option>Armenia</option>
</select>
</body></html>

这对我来说很好,生成一个带有三个选项的下拉框 - 选择您的国家阿富汗亚美尼亚。指向上述Python脚本在此文件正确选择亚美尼亚

This works fine for me, producing a dropdown box with three options - Select Your Country, Afghanistan and Armenia. Pointing the above Python script at this file correctly selects Armenia.

编辑:这是一个快速而肮脏的Python从国家列表中成功选择亚美尼亚的脚本:

Here's a quick-and-dirty Python script that successfully selects Armenia from the list of countries:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get("http://ds.arm.com/rfq/")

country = "Armenia"

dropdown_path = "id('content')/form/fieldset/div[5]/div[2]/div/a[2]"
country_path = "id('content')/form/fieldset/div[5]/div[2]/div/ul/li[contains(text(), '%s')]" % country

browser.find_element_by_xpath(dropdown_path).click()
browser.find_element_by_xpath(country_path).click()

它利用XPath找到下拉箭头和然后是包含亚美尼亚的< li> 标签。我不认为它是特别直观的,或整洁的看,但它的作品。他们对该网站真的太复杂了,所以我不知道如果更简单的选择方法可以在这里工作。一般来说,如果你可以使用id( find_element_by_id )找到一个元素,class( find_element_by_class )或name code> find_element_by_name ),你应该这样做。没有一个在这里工作,不幸的是:)

It makes use of XPaths to locate the dropdown arrow and then the <li> tag containing "Armenia". I don't claim that it's especially intuitive, or neat to look at, but it works. They've really over-complicated things with that website, so I'm not sure if the simpler Select method can be made to work here. In general, if you can locate an element using an id (find_element_by_id), class (find_element_by_class) or name (find_element_by_name), you should do so. None of those work here, unfortunately :)

我可以推荐 Firefox的XPath Checker 扩展,以帮助您找到其他表单元素的类似XPath。祝你好运,我真的不羡慕你必须和那个网站一起工作!!

I can recommend the XPath Checker extension for Firefox to help you find similar XPaths for the other form elements. Good luck, I really don't envy you having to work with that site!!

这篇关于如何从硒中选择自定义下拉列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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