python自动化selenium使用的简单记录
**起因:**很早以前就一直想亲自完成表单的自动填报。一直用requests模块的post方法进行尝试,但是一直失败。知道前几天看到了python自动化,这才意识到python提交表单这么“简单”。
阶段一:使用Selenium IDE进行自动化填报(我使用的是Firefox浏览器)
1.火狐浏览器插件搜索Selenium IDE会有一个如图所示的插件,我们将它下载并添加,这样就可以通过这个插件很容易的完成表单的自动化填报。
2.单击运行,选择第一项新创建一个项目:
3.给项目起一个名字
4.输入网址(以凤凰网为例)
5.点击开始,这样在新弹出的窗口点击的事件都会被记录下来
6.通过播放按钮
7.让浏览器自动完成你刚刚执行的操作。
8.在左边的项目栏你可以选择Export将其导出(我选择的是py文件)之后在这个基础上修改代码完成python自动化就非常的便捷。
阶段二:修改导出代码
这是导出的代码:
# Generated by Selenium IDE
import pytest
import time
import json
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 TestUntitled():
def setup_method(self, method):
self.driver = webdriver.Firefox()
self.vars = {
}
def teardown_method(self, method):
self.driver.quit()
def test_untitled