[英]Selenium Webdriver Python 2.x - select value from the list
I have problem with select value from list in Selenium Webdriver and Python 2.x. 我在Selenium Webdriver和Python 2.x中从列表中选择值有问题。 Look at HTML code of my test site and below is my Selenium Webdriver code with my problem. 查看我的测试站点的HTML代码,下面是我的问题的Selenium Webdriver代码。
HTML from my test site (allegro.pl) 来自我的测试网站的HTML(allegro.pl)
<div class="input-text-wrapper"> <input id="main-search-text" type="text" name="string" x-webkit-speech autocomplete="off" required="required" class="autofocus" value="" placeholder="czego szukasz?"/> </div> <select class="search-type-select" name="search_scope"> <option value="">wszystkie działy</option> <option value="electronics">Elektronika</option> <option value="fashion.beauty">Moda i uroda</option> <option value="household.health">Dom i zdrowie</option> <option value="baby">Dziecko</option> <option value="culture.entertainment">Kultura i rozrywka</option> <option value="sports.leisure">Sport i wypoczynek</option> <option value="automotive">Motoryzacja</option> <option value="collections.art">Kolekcje i sztuka</option> <option value="company.services">Firma</option> <option value="user">Użytkownicy</option> <option value="closed">Zakończone</option> <option class="custom-search-type"></option> </select> <input type="submit" value="Szukaj" class="sprite search-btn"/> <div class="search-type-select" > <ul> <li class=""><a class="active" href="#">wszystkie działy</a></li> <li class="department"><a href="#electronics">Elektronika</a></li> <li class="department"><a href="#fashion.beauty">Moda i uroda</a></li> <li class="department"><a href="#household.health">Dom i zdrowie</a></li> <li class="department"><a href="#baby">Dziecko</a></li> <li class="department"><a href="#culture.entertainment">Kultura i rozrywka</a></li> <li class="department"><a href="#sports.leisure">Sport i wypoczynek</a></li> <li class="department"><a href="#automotive">Motoryzacja</a></li> <li class="department"><a href="#collections.art">Kolekcje i sztuka</a></li> <li class="department"><a href="#company.services">Firma</a></li> <li class="separator-top"><a href="#user">Użytkownicy</a></li> <li class=""><a href="#closed">Zakończone</a></li> </ul> <strong data-filter-id="" title="wszystkie działy"> <span>wszystkie działy</span> <i class="sprite"></i> </strong> </div>
My Selenium Webdriver code: 我的Selenium Webdriver代码:
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("http://www.allegro.pl")
searchbox = driver.find_element_by_id("main-search-text")
searchbox.send_keys("what you search")
#And here is my problem, i wanna choose something from the list:
searchlist = find_element_by_css_selector("select#search_scope/option[text()='Elektronika']").click()
#I try another think here, but also not working:
#Select(driver.find_element_by_css_selector("select#search_scope")).select_by_value(baby).click()
I seen in location bar "search_scope", but not value was selected. 我在位置栏“search_scope”中看到,但没有选择值。
The syntax you want to use webelement#webelement_name
is applicable for id
attribute only, but not name
: webelement#webelement_id
. 您要使用的语法webelement#webelement_name
仅适用于id
属性,但不适用于name
: webelement#webelement_id
。
So this "select#search_scope"
means search for a <select id="search_scope">
which in your case, considering provided HTML
sample, should return False
所以这个"select#search_scope"
意味着搜索<select id="search_scope">
在你的情况下,考虑提供HTML
样本,应该返回False
Try to use Select()
as below: 尝试使用Select()
,如下所示:
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_css_selector("select[name='search_scope']"))
select.select_by_visible_text('Elektronika')
If this approach doesn't work, try: 如果此方法不起作用,请尝试:
driver.find_element_by_xpath('//span[text()="wszystkie dzialy"]').click()
driver.find_element_by_link_text('Elektronika').click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.