第一个python自动化实例
-
下载python安装
-
验证是否安装pip
python -m pip --version
输出如下,表示安装
pip 19.2.3 from C:\_Program\Python27\lib\site-packages\pip (python 2.7)
- 安装selenium
pip install selenium
-
查看chrome版本,然后到https://chromedriver.storage.googleapis.com/index.html下载对应的win32.zip
解压zip文件,放在chrome浏览器根目录下
验证是否安装:直接在cmd目录下执行chromedriver即可 -
配置环境变量,将chrome根目录和python根目录添加到环境变量中。
-
添加test.py文件,代码如下
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class TestTest01():
def __init__(self):
self.driver = webdriver.Chrome()
self.vars = {}
def over(self):
self.driver.quit()
def test_test01(self):
self.driver.get("https://www.baidu.com/")
self.driver.set_window_size(1088, 816)
self.driver.find_element(By.ID, "kw").send_keys("selenium")
self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)
obj = TestTest01()
obj.test_test01()
# obj.over()
- cmd窗口运行命令
python test.py