-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path去哪儿网
51 lines (51 loc) · 2.57 KB
/
去哪儿网
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('Chrome/88.0.4324.150')
driver = Chrome('K:/zhusc/chromedriver.exe', options=chrome_options)
with open('K:/zhusc/stealth.min.js') as f:
js = f.read()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})
def wait_for_you(myDynamicElement):
try:
#查看10秒内是否出现该元素,如果元素10秒内出现就返回,否则抛出异常。
element=WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.XPATH,myDynamicElement))
)
finally:
driver.find_element_by_xpath(myDynamicElement).click()
def inputme(s,r): #记事本,输入内容
f=open(r,'a+')
f.writelines(s)
f.write('\n')
url="https://flight.qunar.com/site/oneway_list.htm?searchDepartureAirport=%E5%8C%97%E4%BA%AC&searchArrivalAirport=%E4%B8%8A%E6%B5%B7&searchDepartureTime=2021-02-12&searchArrivalTime=2021-02-14&nextNDays=0&startSearch=true&fromCode=BJS&toCode=SHA&from=flight_dom_search&lowestPrice=null"
driver.get(url)
driver.maximize_window() #窗口最大化
wait_for_you('//*[@id="QunarPopBoxcelogin"]')#短信验证码登录
#请输入手机号
driver.find_element_by_xpath('//*[@id="QunarPopBox"]/div/div[3]/div[2]/form/div[7]/div/input').send_keys('189XXXXXXXX')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="QunarPopBox"]/div/div[3]/div[2]/form/div[9]/span/a').click()#获取验证码
# time.sleep(1)
# driver.find_element_by_xpath('//*[@id="QunarPopBox"]/div/div[3]/div[2]/form/div[9]/div/input').click()#请输入验证码
# a=""
# while len(a)!=6: #当验证码输入至第6位时
# a=driver.find_element_by_xpath('//*[@id="QunarPopBox"]/div/div[3]/div[2]/form/div[9]/div/input').get_attribute('value')#获取验证码输入框内容
# driver.find_element_by_xpath('//*[@id="QunarPopBox"]/div/div[3]/div[2]/form/div[12]/button').click()#登录
myDynamicElement='//*[@id="content"]/div/div[4]/div[3]/div/div/div[1]/div/div[1]/div/div[1]/div/div[1]/span'
try:
#查看10秒内是否出现该元素,如果元素10秒内出现就返回,否则抛出异常。
element=WebDriverWait(driver,360).until(
EC.presence_of_element_located((By.XPATH,myDynamicElement))
)
finally:
a=driver.page_source
print(a)
inputme(a,'k:/zhusc/1.txt')