快速抢票使用selenium


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()

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值