1.环境准备
我的本机环境装的是 python3 ,使用命令python3 -m pip install selenium
安装的 selenium,版本是 selenium (3.9.0)
调用火狐浏览器需要使用 geckodriver 插件,下载地址:https://github.com/mozilla/geckodriver/releases ,解压到火狐的安装目录,然后把该目录加入环境变量 python.
调用IE浏览器需要使用 IEDriverServer插件,下载地址:http://selenium-release.storage.googleapis.com/index.html,解压到IE的安装目录,然后把该目录加入环境变量 python.
2.小脚本
调用火狐浏览器:python3
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://cn.bing.com/")
assert'Bing' in driver.title
driver.quit()
调用IE浏览器:python3
from selenium import webdriver
driver = webdriver.Ie()
driver.get('http://www.baidu.com')
print(dirver.title)
driver.quit()
一步一步执行脚本,就可以看到对应浏览器的响应和反馈了。
另附其他几个WebDriver的小脚本
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://cn.bing.com/")
assert'Bing' in driver.title
driver.quit()
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://cn.bing.com/")
driver.find_element_by_id("sb_form_q").send_keys("WebDriver")
driver.find_element_by_id("sb_form_go").click()
driver.quit()
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://192.168.***.***:80**/bms/logon.action?loginSuccess=true")
driver.find_element_by_id("loginName").send_keys("gaoyx")
driver.find_element_by_id("password").send_keys("***")
driver.switch_to.window(driver.window_handles[2])
driver.find_element_by_name("submit").click()
driver.quit()
from selenium import webdriver
driver = webdriver.Firefox()
driver = webdriver.Chrome('C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe')
driver.get("http://192.168.2***:80**/bms/logon.action?loginSuccess=true")
driver.find_element_by_id("loginName").send_keys("gaoyx")
driver.find_element_by_id("password").send_keys("***")
driver.find_element_by_name("submit").click()
driver.quit()