簡體   English   中英

Selenium Webdriver Python 2.x - 從列表中選擇值

[英]Selenium Webdriver Python 2.x - select value from the list

我在Selenium Webdriver和Python 2.x中從列表中選擇值有問題。 查看我的測試站點的HTML代碼,下面是我的問題的Selenium Webdriver代碼。

來自我的測試網站的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> 

我的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()

我在位置欄“search_scope”中看到,但沒有選擇值。

您要使用的語法webelement#webelement_name僅適用於id屬性,但不適用於namewebelement#webelement_id

所以這個"select#search_scope"意味着搜索<select id="search_scope">在你的情況下,考慮提供HTML樣本,應該返回False

嘗試使用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')

如果此方法不起作用,請嘗試:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM