简体   繁体   English

Selenium Webdriver Python 2.x - 从列表中选择值

[英]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属性,但不适用于namewebelement#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.

相关问题 Python Selenium Webdriver选择下拉值 - Python Selenium Webdriver Select Dropdown Value 使用Python和Selenium Webdriver,根据文本值从运行时生成的复选框列表中选择一个复选框 - Select a check box from a list of run time generated check boxes on the basis of a text value, using Python and Selenium Webdriver 如何使用 python 从 selenium webdriver 的下拉列表中选择一个值? - How do I select a value from drop down in selenium webdriver using python? 即使元素可见,也无法使用python selenium webdriver从下拉列表中选择值 - Unable to select value from drop down with python selenium webdriver even when elements are visible Selenium Webdriver - Python - leboncoin (classified) - pb 选择下拉列表和框 - Selenium Webdriver - Python - leboncoin (classified) - pb to select a dropdown list & box Python(2.x)-从字典列表中的键中删除字符 - Python (2.x) - Remove character from key in list of dicts Selenium的Python Webdriver - Python webdriver from Selenium Selenium webdriver 从 find_elements_by_X 返回空列表 - Selenium webdriver returns empty list from find_elements_by_X 无法从Selenium Python脚本中的列表中选择值 - Unable to select value in a list from selenium python script 使用Python + Selenium WebDriver选择一个段落 - Select a paragraph with Python + Selenium WebDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM