Python Selenium - 灵活列表中的所有选项

时间:2018-01-10 17:30:37

标签: python selenium selenium-webdriver drop-down-menu

我是python的新手并且在这个论坛上搜索了一天但是找不到我的问题的答案。

我正在尝试从灵活的下拉列表中检索所有元素(元素仅在courser结束时显示),将它们保存在列表中,然后使用列表单击其中的每个元素以打开底层网站。

这是我的网站:http://avi2.osamsterdam.nl/index.mpl

我可以选择第一个下拉列表的元素,然后单击打开第二个选择框,但是我被卡住了,因为它们没有类(class ="")。知道我怎么能抓住"它们?

我已经尝试了选择。,但无法选择项目......

我的代码:

navigate_to_website(driver, website) # open website with driver 
# open fist dropdown list:
area = driver.find_element_by_id("geo") 
area.click() 
# find the first choice:
select = [x for x in area.find_elements_by_class_name("expandable")] 
for element in select:
    print (element.text) #options to choose from
    if element.text == "Buurt": #option desired
        element.click()
        break
# it does not work as of here: 
buurt = [x for x in element.find_element_by_link_text('javascript:void')]
for buurten in buurt:
  print (buurten.text)

如果您需要更多信息,请告诉我们。

以下是下拉列表的示例: screenshot

提前谢谢。

1 个答案:

答案 0 :(得分:0)

希望此代码可以帮助您:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

url="http://avi2.osamsterdam.nl/index.mpl"

locatorOne='//a[@class="expandable" and contains(text(),"Amsterdam")]'
locatorTwo='//a[@class="expandable" and contains(text(),"Stadsdeel")]'
locatorThree='//a[@class="expandable" and contains(text(),"Buurt")]'

driver = webdriver.Chrome()

driver.maximize_window()

driver.get(url)

area = driver.find_element_by_id("geo")
area.click()

#to select the option
option = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, locatorThree)))
ActionChains(driver).move_to_element(option).perform()


thirdOption = driver.find_element_by_xpath('//*[@id="geo"]/li/ul/li[3]')
ele=thirdOption.find_elements_by_tag_name("li")
for e in ele:
    print e.text

它将打印Buurt的所有元素文本:

Burgwallen-Oude Zijde
Burgwallen-Nieuwe Zijde
Grachtengordel-West
Grachtengordel-Zuid
Nieuwmarkt/Lastage
Haarlemmerbuurt
Jordaan
De Weteringschans
Weesperbuurt/Plantage
Oostelijke Eilanden/Kadijken
Spaarnd.- en Zeeheldenbuurt
Staatsliedenbuurt
Centrale Markt/Fred. Hendrikbrt
Da Costabuurt
Kinkerbuurt
Van Lennepbuurt
Helmersbuurt
Overtoomse Sluis
Indische Buurt West
Indische Buurt Oost
Oostelijk Havengebied
IJburg West
Landlust
Erasmuspark
De Kolenkit
Geuzenbuurt
Chassébuurt
Van Galenbuurt
Hoofdweg e.o.
Westindische Buurt
Volewijck
IJplein/Vogelbuurt
Tuindorp Nieuwendam/- Buiksloot
Tuindorp Oostzaan
Oostzanerwerf/Kadoelen
Nieuwendam-Noord
Buikslotermeer
Banne Buiksloot
Waterland/Ndamham/dijken
Slotermeer-Noordoost
Slotermeer-Zuidwest
Geuzenveld
Eendracht
Lutkemeer/Ookmeer
Osdorp-Oost
Osdorp-Midden
De Punt
MAP/Sloten/Lutke-/Ookmeer
Slotervaart
Overtoomse Veld
Westlandgracht
Sloter-/Riekerpolder
Bijlmer Centrum (D,F,H)
Bijlmer Oost (E,G,K)
Nellestein
Holendrecht/Reigersbos
Gein
Driemond
Weesperzijde
Oosterparkbuurt
Dapperbuurt
Transvaalbuurt
Frankendael
Middenmeer
Betondorp/De Omval
Oude Pijp
Nieuwe Pijp
Diamantbuurt
Hoofddorppleinbuurt
Schinkelbuurt
Willemspark
Museumkwartier/Duivelseiland
Stadionbuurt
Apollobuurt
Scheldebuurt
IJselbuurt
Rijnbuurt
Buitenveldert-West/Zuid/WTC
Buitenveldert-Oost