#-*-coding:utf-8 -*-# author:amyfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver import ActionChainsimport timedef login(driver): driver.maximize_window() driver.get("https://passport.ctrip.com/user/login?BackUrl=https%3A%2F%2Fwww.ctrip.com%2F") # 登录页面 time.sleep(4) driver.find_element_by_id("nloginname").send_keys("1977") # 用户名 driver.find_element_by_id("npwd").clear() driver.find_element_by_id("npwd").send_keys("xig") # 密码 time.sleep(2) move=xpath(driver,'/html/body/div[3]/div[2]/div/div[1]/div[1]/dl[3]/dd/div[1]/div[4]') # 携程网登陆页面,滑动验证码参考 if move.is_displayed(): # 验证码是否存在 action = ActionChains(driver) source = driver.find_element_by_xpath('//*[@id="sliderddnormal"]/div[1]/div[2]') # 需要滑动的元素 action.click_and_hold(source).perform() # 鼠标左键按下不放 action.move_by_offset(255, 0) # 需要滑动的坐标 action.release().perform() # 释放鼠标 time.sleep(2) print('滑动成功') driver.find_element_by_id("nsubmit").click() # 点击登录按钮 print("登录成功!") driver.switch_to.default_content()#搜索票def idsendkey(driver,element,keys): driver.find_element_by_id(element).send_keys(keys)def idclick(driver,ele1): driver.find_element_by_id(ele1).click()def id(driver,ele1): driver.find_element_by_id(ele1)def cssselect(driver,ele2): driver.find_element_by_css_selector(ele2).click()def css(driver,ele): driver.find_element_by_css_selector(ele)def xpath(driver,ele3): driver.find_element_by_xpath(ele3)def search(driver,go,arrive): # ii=css(driver,"#nav_trains") # ActionChains(driver).move_to_element(ii).perform() # idclick(driver,"c_ph_train_t") idsendkey(driver,"notice01", go+Keys.ENTER) idsendkey(driver,"notice08", arrive+Keys.ENTER) time.sleep(1) driver.execute_script("document.getElementById('dateObj').value='2019-1-20'") # 输入日期 time.sleep(1) driver.find_element_by_class_name("current").click()#国内火车票空白处点击 idclick(driver,"appd_wrap_close") # 关闭移动手机APP弹框 time.sleep(1) cssselect(driver,"#searchbtn") # 点击搜索按钮 time.sleep(4) xpath(driver,'//*[@id="tbody-01-G70722"]/div[1]/div[6]/div[1]/a').click() # 点击预订按钮 老师你好: 写代码,系统报错提示is_displayed()方法不存在
在学课程: Python Selenium3项目实战 http://www.atstudy.com/course/1005
|