Open 12306 https://kyfw.12306.cn/otn/leftTicket/init
Fill in the departure city 'Nanjing South', fill to reach the city 'Hangzhou East'
Note Enter the name of the city before, be sure to click the input box, or else finding out.
Enter a city name and the last to include a carriage return, otherwise there will automatically clear the input box
Selected departure time 06: 00--12: 00
Departure date of the election of the current time the next day, which is the date of the tab bar, the second label
We want to find all second-class seat tickets as well as trips, print out these trips have the votes of information (where you can use xpath), as follows:
G7641
G1505
G7393
G7689
from selenium import webdriver
from selenium.webdriver.support.ui import Select#Select实例化 先导入select类
driver = webdriver.Chrome(r"D:\for myself\Google\Chrome\Application\chromedriver.exe")
driver.implicitly_wait(10)
driver.get('https://kyfw.12306.cn/otn/leftTicket/init')
fromEle=driver.find_element_by_id('fromStationText')
fromEle.click()
fromEle.clear()
fromEle.send_keys(u'南京南\n')
toEle=driver.find_element_by_id('toStationText')
toEle.click()
toEle.clear ()
toEle.send_keys ( U ' Hangzhou East \ the n- ' )
# select departure time
timeSelect = the Select (driver.find_element_by_id ( 'cc_start_time' ))
# timeSelect.select_by_visible_text ('06: 00--12: 00 ')
timeSelect.select_by_value ( '06.0012 million' )
# select the departure date
driver.find_element_by_css_selector ( '#date_range li: Nth-Child (2)' ) .click ()
# click query
# driver.find_element_by_id ( 'query_ticket') the click ().
Print ( ' \ n-\ n-\ n- =============================== \ n-\ n-\ n- ' )
theTrainLines = Driver .find_elements_by_css_selector('#queryLeftTable > tr')
for one in theTrainLines:
secondlevelseat = one.find_elements_by_css_selector('td:nth-of-type(4)[class]')
if secondlevelseat:
print (one.find_element_by_css_selector('td:nth-of-type(1) a').text)
driver.implicitly_wait(10)
#xpath
driver.quit()
# Method Two: to achieve access to second-class seat tickets of the trips information xpath
print('\n\n\n===============================\n\n\n')
xpath ='//*[@id="queryLeftTable"]//td[4][@class]/../td[1]//a'
theTrains = driver.find_elements_by_xpath(xpath)
for one in theTrains:
print (one.text)