Python自动化难吗?来看上课实况转播!~

论坛 期权论坛 期权     
软件测试大师   2019-7-13 07:54   2456   0

这是之前第二周的分享
概要

课程:python自动化班02天

上课内容:Python-web自动化selenium脚本实战
腾讯课堂链接:https://ke.qq.com/course/368908?tuin=c9595489
思维导图


[h1]主要代码片段[/h1]百度验证码
from selenium import webdriverimport base64import requestsdriver = webdriver.Chrome()
driver.get('https://persons.shgjj.com/')image = driver.find_element_by_id('img1')image.screenshot('./01.png')f = open(r'./01.png', 'rb')# 参数image:图像base64编码img = base64.b64encode(f.read())import requestsurl = "https://aip.baidubce.com/oauth/2.0/tokenquerystring = {"grant_type":"client_credentials","client_id":"1RQYVnqvNBIPoxFtzr68mWzz","client_secret":"NcQfFTMwmNyayuQiLsugfyiP05nPmnKT"}payload = ""headers = {'cache-control': "no-cache",        'Postman-Token': "53654ba8-1fa5-419f-9cf5-d585dd302b5d"
    }    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)# print(response.json())data = response.json()print (data['access_token'])access_token = data['access_token']url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general"querystring = {"access_token":access_token}payload = {"image": img}headers = {    'Content-Type': "application/x-www-form-urlencoded",        'cache-control': "no-cache"
    }    response = requests.request("POST", url, data=payload, headers=headers, params=querystring)print(response.json())checkData = response.json()num = checkData['words_result'][0]['words']driver.find_element_by_id('imagecode1').send_keys(num)注意:如果你的元素截屏位置不对,请将屏幕分辨率设置为 100%

快捷键操作
from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.keys import Keysdriver = webdriver.Chrome()


driver.get('http://39.107.96.138:3000/signin')driver.find_element_by_id('name').send_keys('user1')driver.find_element_by_id('pass').send_keys('123456')driver.find_element_by_css_selector('.span-primary').click()driver.get('http://39.107.96.138:3000/topic/create')input_content = driver.find_element_by_css_selector('.CodeMirror-lines')input_content.click()action = ActionChains(driver)action.move_to_element(input_content).send_keys('abc')action.key_down(Keys.COMMAND)action.send_keys('a')action.key_up(Keys.COMMAND)action.key_down(Keys.COMMAND)action.send_keys('b')action.key_up(Keys.COMMAND)# perform()action.perform()# driver.find_element_by_class_name('eicon-image').click()# driver.find_element_by_class_name('webuploader-element-invisible').send_keys('/Users/zyzhao/Desktop/2019-01-13-selenium02.png')
拖拽
from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.keys import Keysdriver = webdriver.Chrome()


driver.get('https://login.zhipin.com/?ka=header-login')span = driver.find_element_by_css_selector('form>div:nth-child(4) div.nc_scale > span.nc_iconfont.btn_slide')action = ActionChains(driver)action.move_to_element(span)action.click_and_hold()action.move_by_offset(495,0)action.release()action.perform()
iframe 切换
from selenium import webdriverimport timedriver = webdriver.Chrome()driver.get('file:///Users/zyzhao/Desktop/20190113/index.html')iframe1 = driver.find_element_by_tag_name('iframe')time.sleep(2)driver.switch_to_frame(iframe1)driver.switch_to_frame('iframeLoginIfm')driver.find_element_by_id('pwdTab').click()driver.find_element_by_id('pwdUserNameIpt').send_keys('12345')driver.find_element_by_id('pwdIpt').send_keys('sddsds')driver.switch_to_default_content()print(driver.page_source)
alert 处理
from selenium import webdriverfrom selenium.webdriver.common.alert import Alertfrom selenium.webdriver.common.by import Bydriver = webdriver.Chrome()driver.get('http://39.107.96.138:3000/signin')driver.find_element_by_id('name').send_keys('user1')driver.find_element_by_id('pass').send_keys('123456')driver.find_element_by_css_selector('.span-primary').click()driver.get('http://39.107.96.138:3000/user/user1')driver.find_element(by=By.CLASS_NAME,'topic_title').click()driver.find_element_by_class_name('topic_title').click()driver.find_element_by_css_selector('i.fa.fa-lg.fa-trash').click()print( Alert(driver).text)# 取消Alert(driver).dismiss()# 确定Alert(driver).accept()
等待时间
from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.implicitly_wait(10)driver.get('https://outlook.live.com/owa/#')driver.find_element_by_css_selector('div > a.linkButtonSigninHeader:nth-child(4)').click()try:    # inputEmail = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.NAME,'loginfmt')))
    inputEmail = WebDriverWait(driver,10).until(lambda driver: driver.find_element_by_name('loginfmt'))   
        inputEmail.send_keys('1321312312')except expression as identifier:    print('time out...')    # driver.find_element_by_name('loginfmt').send_keys('1234455')
javascript
from selenium import webdriverdriver = webdriver.Chrome()driver.get('https://www.ctrip.com/')start_date = '2019-01-20'js_script ='document.querySelector("#HD_CheckIn").value = "{}"'.format(start_date)print(js_script)driver.execute_script(js_script)
手机模拟
from selenium import webdriver# from selenium.webdriver.chrome.options import Options# mobile_emulation = {"deviceName":"iPhone X"}mobile_emulation = {"deviceName":"iPad"}chrome_options = webdriver.ChromeOptions()chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)driver = webdriver.Chrome(options=chrome_options)driver.get('https://www.baidu.com')
微博抓取页面
import xlwt# from datetime import datetime# style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',#     num_format_str='#,##0.00')# style1 = xlwt.easyxf(num_format_str='D-MMM-YY')# ws.write(0, 0, 1234.56, style0)# ws.write(1, 0, datetime.now(), style1)# ws.write(2, 0, 1)# ws.write(555, 1, 1)# ws.write(2, 2, xlwt.Formula("A3+B3"))# wb.save('example.xls')from selenium import webdriverdriver = webdriver.Chrome()driver.maximize_window()driver.implicitly_wait(3)driver.get('https://s.weibo.com/')keys = 'web自动化'wb = xlwt.Workbook()ws = wb.add_sheet(keys)ws.write(0,0,'用户')ws.write(0,1,'内容')ws.write(0,2,"时间")ws.write(0,3,'来源')ws.write(0,4,'收藏')ws.write(0,5,'转发')ws.write(0,6,'评论')ws.write(0,7,'喜欢')driver.find_element_by_css_selector('[type="text"]').send_keys(keys)# driver.find_element_by_css_selector('[type="text"]').submit()driver.find_element_by_class_name('s-btn-b').click()driver.find_element_by_css_selector('[node-type="advsearch"]').click()driver.find_element_by_css_selector('[for="radio03"]').click()driver.find_element_by_css_selector('[node-type="OK"]').click()rowIndex = 1for x in range(5):    users = driver.find_elements_by_css_selector('[action-type="feed_list_item"]')        for user in users:            username = user.find_element_by_xpath('.//*[@class="name"]').text
        content = user.find_element_by_xpath('.//*[@class="txt"]').text
        publish_date = user.find_element_by_xpath('.//*[@class="from"]/a[1]').text
        publish_source = user.find_element_by_xpath('.//*[@class="from"]/a[2]').text

        shoucang = user.find_element_by_xpath('.//*/div[@class="card-act"]/ul/li[1]/a').text
        shoucang =shoucang.split("收藏")[1] or 0
        zhuanfa = user.find_element_by_xpath('.//*/div[@class="card-act"]/ul/li[2]/a').text
        zhuanfa = zhuanfa.split("转发")[1] or 0
        pinglun = user.find_element_by_xpath('.//*/div[@class="card-act"]/ul/li[3]/a').text
        pinglun = pinglun.split("评论")[1] or 0
        xihuan = user.find_element_by_xpath('.//*/div[@class="card-act"]/ul/li[4]/a').text or 0
        # xihuan = xihuan.split("收藏")[1]
        print(username,content,publish_date,publish_source,shoucang,zhuanfa,pinglun,xihuan)                ws.write(rowIndex,0,username)                ws.write(rowIndex,1,content)                ws.write(rowIndex,2,publish_date)                ws.write(rowIndex,3,publish_source)                  ws.write(rowIndex,4,shoucang)                ws.write(rowIndex,5,zhuanfa)                ws.write(rowIndex,6,pinglun)                ws.write(rowIndex,7,xihuan)                        rowIndex+=1

    # 开始点第二页
    xpath = '//*/ul[@class="s-scroll"]/li[{}]'.format(x+2)        print(xpath)        driver.find_element_by_class_name('pagenum').click()        driver.find_element_by_xpath(xpath).click()    wb.save('web.xls')driver.quit()这么多代码的感觉怎么样啊?
大家加油哦,反正小编是晕的不要不要的。

需要浏览视频的可以点击左下角阅读原文即可~

了解更多
Web自动化——开发环境搭建
补充:你面试做对了吗?
如何在一天內入门持续集成Jenkins


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:
帖子:
精华:
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP