Python+Selenium 常見面試題整理

 整理加複習
1、記錄一下剛剛看到的博客https://www.cnblogs.com/lesleysbw/p/5916022.html中的一小段
在這裏插入圖片描述
非常認同

 看到一位大神的面試準備,我不想寫了TT。等我好好看完,再決定怎麼繼續寫下去吧,照搬毫無意義。大神博客地址:https://www.cnblogs.com/lesleysbw/category/946223.html

Qunar機票搜索場景

訪問Qunar機票首頁http://flight.qunar.com,選擇“單程”,輸入出發、到達城市,選擇today+7日後的日期,點“搜索”,跳轉到機票單程搜索列表頁。

好像網上沒有找到python實現,簡單寫了下

from selenium import webdriver
import datetime
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome(r'D:\chromedriver.exe')
driver.maximize_window()
driver.implicitly_wait(8)

driver.get("https://flight.qunar.com/")
single_way = driver.find_element_by_xpath('//*[@id="searchTypeSng"]')#單程
from_city = driver.find_element_by_xpath('//*[@id="dfsForm"]/div[2]/div[1]/div/input')#出發城市
to_city = driver.find_element_by_xpath('//*[@id="dfsForm"]/div[2]/div[2]/div/input')#到達城市
from_date = driver.find_element_by_xpath('//*[@id="fromDate"]')#出發時間

#選擇單程
if (single_way.is_selected()):
    pass
else:
    single_way.click()

#選擇到達城市-上海
action = ActionChains(driver)
action.move_to_element(to_city).click().perform()
driver.find_element_by_xpath("//div[@data-panel='domesticto-flight-hotcity-to']//a[@class='js-hotcitylist' and text()='上海']").click()
driver.implicitly_wait(8)

#選擇出發城市-北京
action.move_to_element(from_city).click().perform()
driver.find_element_by_xpath("//div[@data-panel='domesticfrom-flight-hotcity-from']//a[@class='js-hotcitylist' and text()='北京']").click()
driver.implicitly_wait(8)

#設置出發日期-7天后
date = (datetime.datetime.now() + datetime.timedelta(days=7)).strftime("%Y-%m-%d")
# print(date)
from_date.send_keys(Keys.CONTROL + "a")
# js = "document.getElementById('fromDate').value='2019-02-07'" #編寫JS語句
# driver.execute_script(js) #執行JS
from_date.send_keys(date)

#搜索
driver.find_element_by_xpath('//*[@id="dfsForm"]/div[4]/button').click()
time.sleep(6)

driver.quit()

執行搜索,會出現一直加載中的情況,不知道爲啥,有時候又挺好的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章