| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
Selenium : Web 自动化测试工具,可以按照指定的命令自动操作,Selenium 可以直接运行在浏览器上,支持所有主流浏览器 (包括 PhantomJS 等无界面浏览器)
Selenium 可以根据我们的指令,让浏览器自动加载界面,获取需要的数据,网页截屏,判断网站发生的动作
Selenium不自带浏览器,需要与第三方浏览器结合才可使用,使用方法
使用 PhantomJS 工具代替真实浏览器
下载火狐浏览器的 geckodriver 下载链接 或 谷歌浏览器的 chromedriver (与浏览器版本对应, chromedriver 下载链接构造 https://chromedriver.storage.googleapis.com/index.html?path=谷歌浏览器版本/)
火狐浏览器不弹出页面
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(firefox_options=options, executable_path=ex_path)谷歌浏览器不弹出界面
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image,ImageEnhance
path = 'C:/dd/chromedriver.exe' # chromedriver.exe 驱动的路径
#打开浏览器
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# 创建浏览器对象
driver = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)Selenium 常用方法:
Selenium 操作的重点在于 确定等待时间 以提高效率
强制等待 : import time
import time
time.sleep(3)隐式等待 : driver.implicitly_wait(wait_time)
显式等待 (WebDriverWait) : 等待页面特定元素 (使用元素定位器定位) 满足特定条件 (定位器和特定查询条件) 开始加载
导入 定位器、查询条件、显式等待所用方法
from selenium.webdriver.common.by import By # 定位器
from selenium.webdriver.support import expected_conditions as EC # 查询条件
from selenium.webdriver.support.wait import WebDriverWait # 显式等待创建显式等待对象
wait = WebDriverWait(driver, 20)使用定位器 根据 查询条件 确定加载等待用时
EC.presence_of_element_located(查询方法, 定位语句) : 定位单个元素
EC.presence_of_all_elements_located(查询方法, 定位语句) : 定位多个元素
实例
wait.until(EC.presence_of_all_elements_located(By.XPATH, '等待位置的 XPATH 语句')) # 等到元素出现为止,传入定位器| Back | FazBrowse Home | New Git URL |