016、driver对象 操作 ,获取页面源码(page_source) 和 获取配置相关(capabilities)
一、driver对象 操作 ,获取页面源码(page_source) 和 获取配置相关(capabilities)
1、获取driver对象属性
current_url :当前页面url地址
title :页面左上角title
name :获取当前浏览器名称
page_source :获取页面源码
capabilities: 获取配置相关
a、获取 driver对象 属性
示例代码如下:
from selenium import webdriver driver = webdriver.Chrome() driver.get("http://49.235.92.12:8200/users/login/") title = driver.title print(title) name = driver.name print(name) url = driver.current_url print(url)
driver.quit()
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Web_AutoTest\Temp\day01\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Web_AutoTest/Temp/day01/ch_002/study_18.py
在线学习网登录
chrome
http://49.235.92.12:8200/users/login/
Process finished with exit code 0
b、capabilities: 获取配置相关
示例代码如下:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://49.235.92.12:8200/users/login/")
cap = driver.capabilities
print(cap)
print("浏览器版本号:%s" % cap["browserVersion"])
print("chromedriverVersion:%s" % cap["chrome"]["chromedriverVersion"])
driver.quit()
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Web_AutoTest\Temp\day01\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Web_AutoTest/Temp/day01/ch_002/study_18.py {'acceptInsecureCerts': False, 'browserName': 'chrome', 'browserVersion': '92.0.4515.131',
'chrome': {'chromedriverVersion': '92.0.4515.107 (87a818b10553a07434ea9e2b6dccf3cbe7895134-refs/branch-heads/4515@{#1634})',
'userDataDir': 'C:\\Users\\27806\\AppData\\Local\\Temp\\scoped_dir2448_2012812779'},
'goog:chromeOptions': {'debuggerAddress': 'localhost:51089'},
'networkConnectionEnabled': False, 'pageLoadStrategy': 'normal', 'platformName': 'windows', 'proxy': {}, 'setWindowRect': True,
'strictFileInteractability': False, 'timeouts': {'implicit': 0, 'pageLoad': 300000, 'script': 30000}, 'unhandledPromptBehavior': 'dismiss and notify',
'webauthn:extension:credBlob': True, 'webauthn:extension:largeBlob': True, 'webauthn:virtualAuthenticators': True}
浏览器版本号:92.0.4515.131 chromedriverVersion:92.0.4515.107 (87a818b10553a07434ea9e2b6dccf3cbe7895134-refs/branch-heads/4515@{#1634}) Process finished with exit code 0
c、page_source 获取页面源码——爬取所有的图片
部分代码如下,非完整功能:
from selenium import webdriver
import re
driver = webdriver.Chrome()
driver.get("http://49.235.92.12:8200/users/login/")
# 获取页面的源码
page = driver.page_source
print(page)
# <img width="483" height="472" src="/static/images/kecheng.png">
a = re.findall('<img.*src="(.+?)"', page)
# ['/static/images/kecheng.png', '/static/images/kecheng.png', '/static/images/kecheng.png']
print(a)
driver.quit()
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Web_AutoTest\Temp\day01\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Web_AutoTest/Temp/day01/ch_002/study_18.py <html lang="zh-CN"><head> <meta charset="UTF-8"> <meta charset="UTF-8"> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> <title> 在线学习网登录 </title> <link rel="stylesheet" type="text/css" href="/static/css/reset.css"> <link rel="stylesheet" type="text/css" href="/static/css/login.css"> <link rel="shortcut icon" type="image/x-icon" href="/static/images/favicon.ico"> </head> <body><header> <div class="c-box fff-box"> <div class="wp header-box"> <p class="fl hd-tips"> 最好的在线学习平台! </p> <ul class="fr hd-bar"> <li>服务电话:<span>8888888</span></li> <li><a href="/users/login/">[登录]</a></li> <li class="active"><a href="/users/register/">[注册]</a></li> </ul> </div> </div> </header> <section> <div class="c-box bg-box"> <div class="login-box clearfix"> <div class="hd-login clearfix"> <a class="index-logo" href="/"></a> <h1>用户登录</h1> <a class="index-font" href="/">回到首页</a> </div> <div class="fl slide"> <div class="imgslide has-dots" style="width: 483px; height: 474px; overflow: hidden;"> <ul class="imgs" style="position: relative; left: 0px; width: 300%;"> <li style="float: left; width: 483px;"><a href=""><img width="483" height="472" src="/static/images/kecheng.png"></a></li> <li style="float: left; width: 483px;"><a href=""><img width="483" height="472" src="/static/images/kecheng.png"></a></li> <li style="float: left; width: 483px;"><a href=""><img width="483" height="472" src="/static/images/kecheng.png"></a></li> </ul> <ol class="dots"><li class="dot active">1</li><li class="dot">2</li><li class="dot">3</li></ol></div> <div class="unslider-arrow prev"></div> <div class="unslider-arrow next"></div> </div> <div class="fl form-box"> <h2>帐号登录</h2> <form action="/users/login/" method="post" autocomplete="off"> <div class="form-group marb20 "> <label>用 户 名</label> <input name="username" id="username" type="text" value="" placeholder="请输入您的邮箱地址"> </div> <div class="form-group marb8 "> <label>密 码</label> <input name="password" id="password_l" type="password" value="" placeholder="请输入您的密码"> </div> <div class="error btns login-form-tips" id="jsLoginTips"> </div> <div class="auto-box marb38"> <a class="fr" href="/users/forgetpwd/">忘记密码?</a> </div> <input class="btn btn-green" id="jsLoginBtn" type="submit" value="立即登录 > "> <input type="hidden" name="csrfmiddlewaretoken" value="DI2TNXhvwTBUSYvDhRCh59sNfR53ASAdUTaaydemsFLYQXYyGOZZbYBmV08TkCCU"> </form> <p class="form-p">没有在线网帐号? <a href="/users/register/">[立即注册]</a> <a href="https://www.cnblogs.com/yoyoketang/" target="_blank" name="blog" id="yoyoblog">[联系我们]</a> <a href="/users/feedbackiframe/" name="feedback" id="feedback">[意见反馈]</a> </p> </div> </div> </div> </section> <script src="/static/js/jquery.min.js" type="text/javascript"></script> <script src="/static/js/unslider.js" type="text/javascript"></script> <script src="/static/js/validateDialog.js" type="text/javascript"></script> <script src="/static/js/login.js" type="text/javascript"></script> </body></html> ['/static/images/kecheng.png', '/static/images/kecheng.png', '/static/images/kecheng.png'] Process finished with exit code 0