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

执行搜索,会出现一直加载中的情况,不知道为啥,有时候又挺好的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值