1. 页面等待
1.1 页面等待分类
- 强制等待
time.sleep() - 隐式等待
指定时间内定位元素,在一段时间内定位成功,就进行下一步。如果没有定位成功就会报超时加载
from selenium import webdriver
url = 'https://www.baidu.com'
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get(url)
el = driver.find_element_by_xpath('//*[@id = "lg"]/img[1]')
print(el)
- 显示等待
2. selenium开启无头模式
from selenium import webdriver
url = 'http://www.baidu.com'
opt = webdriver.ChromeOptions()
opt.add_argument('--headless')
opt.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options = opt)
driver.get(url)
driver.save_screenshot('baidu.png')
3. 使用代理ip
from selenium import webdriver
url = 'http://www.baidu.com'
opt = webdriver.ChromeOptions()
opt.add_argument('--proxy-server=http://221.122.91.74:9401')
driver = webdriver.Chrome(chrome_options = opt)
driver.get(url)
4. 替换uesr-agent
from selenium import webdriver
url = 'http://www.baidu.com'
opt = webdriver.ChromeOptions()
opt.add_argument('--user-agent=Mozilla/5.0 python3.8')
driver = webdriver.Chrome(chrome_options = opt)
driver.get(url)