案例:在携程网页面选择指定的房间数和酒店级别。
1. 根据选项元素标签定位
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://www.ctrip.com/")
sleep(4)
# 根据option标签来定位(7间三星级/舒适)
# driver.find_elements_by_tag_name('option')[6].click()
driver.find_elements_by_css_selector("[value='7']")[0].click()
sleep(2)
driver.quit()
2. 使用select类定位
fromselenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
driver = webdriver.Firefox()
driver.get("https://www.ctrip.com/")
sleep(4)
# 利用Select类来定位(7间三星级/舒适)roomCount=Select(driver.find_element_by_css_selector("#J_roomCountList"))
# roomCount.select_by_index(6)
# roomCount.select_by_visible_text("7间")
roomCount.select_by_value("7")
star = Select(driver.find_element_by_css_selector("[name='Star']"))
# star.select_by_index(3)
# star.select_by_visible_text("三星级/舒适")
star.select_by_value("3")
sleep(2)
driver.quit()