简体   繁体   English

如何使用 Selenium 和 Python 从下拉菜单中选择 select 选项

[英]How to select an option from the dropdown menu using Selenium and Python

I am trying to get the fares for all combinations of stations in Metro-North using Selenium in Python.我正在尝试使用 Python 中的 Selenium 获取 Metro-North 中所有车站组合的票价。 I wanted to go to their fare page, put station names into the select fields, click on the fare button, and then copy the needed values to the dataframe.我想 go 到他们的票价页面,将车站名称放入 select 字段,单击票价按钮,然后将所需的值复制到 Z6A8064B5DF479455500553C47C5505DZ。

I tried all possible options to select stations from the dropdown menu but nothing works as I get the error: ElementNotInteractableException .我从下拉菜单中尝试了 select 站的所有可能选项,但没有任何效果,因为我收到错误: ElementNotInteractableException

The code I tried:我试过的代码:

driver = webdriver.Safari()
driver.get('http://as0.mta.info/mnr/schedules/sched_form.cfm')

select = Select(driver.find_element_by_id('Vorig_station'))
print([o.text for o in select.options])
time.sleep(3)
select.select_by_visible_text('ANSONIA')
element = driver.find_element_by_xpath('//*[@id="frmindex"]/table[2]/tbody/tr[6]/td/input[2]')
element.click()
driver.close()

print command shows the options but I cannot choose and go to the next fares page.打印命令显示选项,但我无法选择和 go 到下一个票价页面。

Help please!请帮忙!

To get the fares for all combinations of stations in Metro-North using Selenium and selecting the option ANSONIA from the From Staion you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies :要使用SeleniumMetro-North 中所有车站组合的票价,从From Staion 中选择选项ANSONIA ,您需要为element_to_be_clickable()诱导WebDriverWait ,您可以使用以下定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     driver.get("http://as0.mta.info/mnr/schedules/sched_form.cfm") WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#Vorig_station"))).click() select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#Vorig_station")))) print([o.text for o in select.options]) select.select_by_visible_text('ANSONIA') WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='fares']"))).click()
  • Using XPATH :使用XPATH

     driver.get("http://as0.mta.info/mnr/schedules/sched_form.cfm") WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='Vorig_station']"))).click() select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='Vorig_station']")))) print([o.text for o in select.options]) select.select_by_visible_text('ANSONIA') WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='fares']"))).click()
  • Console Output:控制台 Output:

     ['ANSONIA', 'APPALACHIAN TRAIL', 'ARDSLEY-ON-HUDSON', 'BEACON', 'BEACON FALLS', 'BEDFORD HILLS', 'BETHEL', 'BOTANICAL GARDEN', 'BRANCHVILLE', 'BREAKNECK RIDGE', 'BREWSTER', 'BRIDGEPORT', 'BRONXVILLE', 'CANNONDALE', 'CHAPPAQUA', 'COLD SPRING', 'CORTLANDT', 'COS COB', 'CRESTWOOD', 'CROTON FALLS', 'CROTON-HARMON', 'DANBURY', 'DARIEN', 'DERBY', 'DOBBS FERRY', 'DOVER PLAINS', 'EAST NORWALK', 'FAIRFIELD', 'FAIRFIELD METRO', 'FLEETWOOD', 'FORDHAM', 'GARRISON', 'GLENBROOK', 'GLENWOOD', 'GOLDENS BRIDGE', 'GRAND CENTRAL', "GREEN'S FARMS", 'GREENWICH', 'GREYSTONE', 'HARLEM - 125TH ST.', 'HARLEM VALLEY-WINGDALE', 'HARRISON', 'HARTSDALE', 'HASTINGS-ON-HUDSON', 'HAWTHORNE', 'IRVINGTON', 'KATONAH', 'LARCHMONT', 'LUDLOW', 'MAMARONECK', 'MANITOU', 'MARBLE HILL', 'MEADOWLANDS SPORTS COMPLEX', 'MELROSE', 'MERRITT 7', 'MILFORD', 'MORRIS HEIGHTS', 'MOUNT KISCO', 'MOUNT PLEASANT', 'MT VERNON EAST ', 'MT VERNON WEST', 'NAUGATUCK', 'NEW CANAAN', 'NEW HAMBURG', 'NEW HAVEN', 'NEW ROCHELLE', 'NH-STATE ST.', 'NOROTON HEIGHTS', 'NORTH WHITE PLAINS', 'OLD GREENWICH', 'OSSINING', 'PATTERSON', 'PAWLING', 'PEEKSKILL', 'PELHAM', 'PHILIPSE MANOR', 'PLEASANTVILLE', 'PORT CHESTER', 'POUGHKEEPSIE', "PURDY'S", 'REDDING', 'RIVERDALE', 'RIVERSIDE', 'ROWAYTON', 'RYE', 'SCARBOROUGH', 'SCARSDALE', 'SEYMOUR', 'SOUTH NORWALK', 'SOUTHEAST', 'SOUTHPORT', 'SPRINGDALE', 'SPUYTEN DUYVIL', 'STAMFORD', 'STRATFORD', 'TALMADGE HILL', 'TARRYTOWN', 'TENMILE RIVER', 'TREMONT', 'TUCKAHOE', 'UNIVERSITY HEIGHTS', 'VALHALLA', 'WAKEFIELD', 'WASSAIC', 'WATERBURY', 'WEST HAVEN', 'WESTPORT', 'WHITE PLAINS', 'WILLIAMS BRIDGE', 'WILTON', 'WOODLAWN', 'YANKEES-E153 ST.', 'YONKERS']
  • Note : You have to add the following imports:注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import Select
  • Browser Snapshot:浏览器快照:

火车票价

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium 和 Python 从下拉菜单中选择 select 选项 - How to select an option from a dropdown menu through partial text using Selenium and Python SELECT 使用 Selenium 的下拉选项 Python - SELECT option from dropdown using Selenium Python 如何在 python 中使用 selenium 从下拉列表中选择 select - How to select an option from dropdown using selenium in python 如何使用 Selenium Python 选择下拉菜单 - How to select a dropdown menu using Selenium Python 如何使用Selenium在python中选择下拉菜单 - How to select dropdown menu in python using Selenium 如何使用 Selenium Python 在没有“选择”标签的下拉菜单中单击选项? - How to click option in dropdown menu which does not have "select" tag using Selenium Python? 如何使用 Python 在 Selenium 的下拉菜单中打印预选选项的文本? - How to print the text of the preselected option in a dropdown menu with Selenium using Python? 如何使用 Selenium 和 Python 提取下拉菜单的选定选项的文本 - How to extract the text of the selected option of a Dropdown Menu using Selenium and Python 如何从 python selenium 的下拉菜单中单击一个选项? - How can I click on an option from a dropdown menu in python selenium? 使用 selenium 和 python 从下拉按钮中选择选项 - Select option from a dropdown button using selenium with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM