自动化框架selenium模块webdriver的使用
安装:
安装selenium库
pip install selenium
安装各浏览器的驱动
Chrome(谷歌浏览器/谷歌内核浏览器)的web driver(chromedriver.exe)
查看Chrome浏览器版本,ip窗口输入chrome://version/
对应浏览器版本号下载(放在Chrome安装目录):
http://chromedriver.storage.googleapis.com/index.html
firefox(火狐浏览器)的web driver (geckodriver.exe)
对应电脑系统和环境下载(放在python下的Scripts的子目录):
https://github.com/mozilla/geckodriver/releases
使用:
1 |
|
webdriver常用方法函数:
注意:
当网页为完全打开的情况下定位网页元素可能会失败
#1.等待几秒
import time
time.sleep(3)
#2.捕获异常
try:
find_element_by_xpath("/html/body/div[7]/input[1]")
a = True
except:
a = False
#3.使用WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
element = WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located(By.ID,"kw"))
自动化框架selenium模块webdriver的使用
https://www.inktea.eu.org/2021/43764.html