PHP高级工程师|全栈工程师|WX: Aben_Ring

第一个python自动化实例

  1. 下载python安装

  2. 验证是否安装pip

python -m pip --version
输出如下,表示安装
pip 19.2.3 from C:\_Program\Python27\lib\site-packages\pip (python 2.7)
  1. 安装selenium
pip install selenium
  1. 查看chrome版本,然后到https://chromedriver.storage.googleapis.com/index.html下载对应的win32.zip
    解压zip文件,放在chrome浏览器根目录下
    验证是否安装:直接在cmd目录下执行chromedriver即可

  2. 配置环境变量,将chrome根目录和python根目录添加到环境变量中。

  3. 添加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()
  1. cmd窗口运行命令
python test.py
posted @ 2022-07-20 18:05  懒牛拉车  阅读(58)  评论(0编辑  收藏  举报