from selenium import webdriver#驱动浏览器
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By#选择器
from selenium.webdriver.common.keys import Keys#按键
from selenium.webdriver.support.wait import WebDriverWait#等待页面加载完毕
from selenium.webdriver.support import expected_conditions as EC#等待指定标签加载完毕
from selenium.common.exceptions import TimeoutException, NoSuchElementException#知识点:异常处理模块
import time,random
from lxml import etree
class huoche(object):
def __init__(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.start_url='https://user.qunar.com/passport/login.jsp?ret=https%3A%2F%2Fwww.qunar.com%2F%3Fex_track%3Dauto_4e0d874a'
def get_data(self):
self.driver.get(self.start_url)
self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/div/div[1]/div[1]/div[2]').click()
time.sleep(2)
self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/div/div[1]/div[3]/div/div[1]/div/input').send_keys('账号')
time.sleep(2)
text_password=self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/div/div[1]/div[3]/div/div[2]/div/input')
text_password.send_keys('密码')
text_confirm=self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/div/div[1]/div[4]/input').click()
self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/div/div[1]/div[3]/div/div[3]/span').click()
time.sleep(2)
div_size_1=self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/div/div[1]/div[3]/div/div[5]/div/div/div[3]/div[2]')
# "//*[@id="app"]/div/div[2]/div/div[1]/div[3]/div/div[5]/div/div/div[3]/div[2]"
div_size=div_size_1.size
#print(div_size)
button=self.driver.find_element(By.XPATH,'//*[@id="app"]/div/div[2]/div/div[1]/div[3]/div/div[5]/div/div/div[3]/div[3]/i')
button_location=button.location
#print(button_location)#345
#ActionChains可以控制鼠标,鼠标的按钮操作,上下移动
self.action=ActionChains(self.driver)
self.action.click_and_hold(button).perform()
self.action.move_by_offset(512,355)
self.action.release().perform()
time.sleep(2)
self.driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[1]/ul/li[4]/a/span/b').click()
self.driver.implicitly_wait(10)
#输入出发地址
departure=self.driver.find_element(By.XPATH,'//*[@id="js-con"]/div[1]/form/div[1]/div[1]/div[1]/div/div/input')
departure.send_keys('乌鲁木齐')
#输入目的地
arrive=self.driver.find_element(By.XPATH,'//*[@id="js-con"]/div[1]/form/div[1]/div[1]/div[2]/div/div/input')
arrive.send_keys('库尔勒')
#点击搜索按钮
self.driver.implicitly_wait(10)
self.driver.find_element(By.XPATH,'//*[@id="js-con"]/div[1]/form/div[2]/div/span/button').click()
#删掉原有的日期
self.driver.find_element(By.XPATH,'//*[@id="js-con"]/div[1]/form/div[1]/div[2]/div/div/div[1]/input').input.clear()
#输入日期
self.driver.implicitly_wait(10)
set_out_time=self.driver.find_element(By.XPATH,'//*[@id="js-con"]/div[1]/form/div[1]/div[2]/div/div/div[1]/input')
set_out_time.send_keys('2021-12-26')
self.driver.find_element(By.XPATH,'//*[@id="7dayNext"]').click()
#//*[@id="7dayNext"]
#点那个按钮找到,按钮,然后换日期,点24次
# for i in range(24):
# self.driver.implicitly_wait(10)
# self.driver.find_element(By.XPATH,'//*[@id="7dayNext"]').click()
# self.driver.implicitly_wait(10)
# #然后点击抢票
# self.driver.find_element(By.XPATH,'//*[@id="7dayUl"]/li[32]/a').click()
# self.driver.implicitly_wait(10)
# # 点击(抢票按钮)
# self.driver.find_element(By.XPATH,'//*[@id="list_listInfo"]/ul[2]/li[1]/div/div[7]/a[1]/span').click()
# time.sleep(3)
# # 点击(选好了按钮)
# self.driver.find_element(By.XPATH,'//*[@id="robContain"]/div[2]/a').click()
# self.driver.implicitly_wait(10)
#
# #self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
# js='scrollTo(0,6000)' #通过js代码向下滑动,起始位置,终止位置
# self.driver.execute_script(js)
# self.driver.implicitly_wait(10)
# self.driver.find_element(By.XPATH,'//*[@id="contact151573595"]').click()
# self.driver.implicitly_wait(10)
# self.driver.find_element(By.XPATH,'//*[@id="fillOrder_grabNormalSubmit"]').click()
if __name__ == '__main__':
h=huoche()
h.get_data()
快速抢票使用selenium
于 2024-05-14 16:27:15 首次发布