2017-07-17 43 views
0

這是skyscanner的網站。選擇航班後,我想點擊每個「選擇按鈕」(687美元),然後轉到下一頁。在下一頁我想點擊向下箭頭進行出境航班。然後我再次點擊選擇按鈕(715美元)和再次出站的飛行箭頭。因此它可以在所有可能的頁面上進行所有可能的搜索循環。瀏覽蟒蛇中的超鏈接

enter image description here

這是下一個頁面上向下的箭頭是如何

enter image description here

這是迄今爲止 我寫的代碼:

driver = webdriver.Chrome() 

driver.get('https://www.skyscanner.com/transport/flights/nyca/lax/170717/170718/airfares-from-new-york-to-los-angeles-international-in-july-2017.html?adults=1&children=0&adultsv2=1&childrenv2=&infants=0&cabinclass=economy&rtn=1&preferdirects=false&outboundaltsenabled=false&inboundaltsenabled=false&ref=home#results') 

while True: 
     items=driver.find_element_by_class_name("fss-bpk-button expand-cba select-action") 
     for i, item in enumerate(items): 
      driver.find_element_by_class_name("fss-bpk-button expand-cba select-action").click() 
       for j, item2 in item 
        driver.find_element_by_class_name("leg-summary-container clearfix").click() 

我也試過以下,但none working:

links = [link.get_attribute('href') for link in driver.find_element_by_class_name("fss-bpk-button expand-cba select-action")] 
    for link in links: 
     driver.get(link) 

回答

0

你的第二個代碼應該有些變化後的工作:

links = [link.get_attribute('href') for link in driver.find_elements_by_css_selector("a.fss-bpk-button.expand-cba.select-action")] 

注意

  1. 要獲得元素的列表,你應該使用的find_elements_...()代替find_element_...()
  2. 不允許複合類的名字,所以你可以使用搜索CSS選擇器或XPath而不是搜索class名稱,如果你想使用multipl e類名稱來定位所需元素
+0

非常感謝!我對Python非常陌生,因此學習了很多!我應該如何在頁面之間循環?我不明白 – Analyst

+0

以同樣的方式你嘗試:'鏈接鏈接:driver.get(鏈接)'...你有鏈接列表。您可以導航到每個頁面,以便在循環中執行所需的操作\ – Andersson