Python Selenium - 灵活列表中的所有选项
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Selenium - 灵活列表中的所有选项相关的知识,希望对你有一定的参考价值。
我是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
先感谢您。
答案
希望此代码可以帮助您:
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
以上是关于Python Selenium - 灵活列表中的所有选项的主要内容,如果未能解决你的问题,请参考以下文章
Selenium Python:选择一个类中的所有项目并将它们放入列表中?
如何使用 Python 使用 Selenium 获取 <ul> 中的 <li> 元素列表?