一。环境部署
1.selenium安装
pip3 install selenium
1.安装浏览器驱动
WebDriver 需要通过浏览器驱动来与浏览器交互,以下列出几种常用的浏览器驱动下载地址:
Chrome:
http://chromedriver.storage.googleapis.com/index.html
Firefox:
https://github.com/mozilla/geckodriver/releases
Edge:
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver
Safari:
https://webkit.org/blog/6900/webdriver-support-in-safari-10
使用不同的浏览器,需要下载对应版本驱动,并且确保驱动文件如chromedriver.exe放到你的系统环境PATH中,这里我将它放到python安装目录下。D:\Python36\chromedriver.exe
selenium思维导图详细使用介绍:
二。元素定位:
1 <!DOCTYPE html> 2 <html lang="zh-CN"> 3 <body> 4 <h1 class="head_title">你好,<span id="world">世界!</span></h1> 5 <a href="https://www.kancloud.cn/@guanfuchang" target="_blank">我的看云首页</a> 6 <br/> 7 <br/> 8 <div id="login_form" class="login"> 9 用户名:<input name="username" class="ptqa user_name" placeholder="用户名" value="Milton" required/> 10 密码:<input type="text" name="password" class="ptqa pwd" placeholder="密码" value="ptqa" required/> 11 地区: 12 <select class="city"> 13 <option value="gz">广州</option> 14 <option value="fs">佛山</option> 15 <option value="mm">茂名</option> 16 <option value="hb">襄樊</option> 17 </select> 18 19 <br> 20 <br> 21 <button>提交</button> 22 <br> 23 24 </div> 25 </body> 26 </html>