目录
13. 修改更改chromedriver的浏览器驱动标识,反爬
6. 等待元素出现 设置了最长等待15秒,超时不出现则抛出异常
有帮助记得点赞呐
13. 修改更改chromedriver的浏览器驱动标识,反爬
url = 'https://xxx.xxx.com'
# 请求页面
driver.get(url)
# 请求页面后执行js,更改chromedriver的浏览器驱动标识,只在当前页面有效
js = 'Object.defineProperty(navigator, "webdriver", {get:() => false});'
brower.execute_script(js)
12. div 滚动条 滚动
# 截取部分代码 driver 就是驱动
# 获取子div的高度
top_length = (div[k].size)['height']
while True:
print('开始滚动')
top_length = (int(top_length) + 500)
# js 获取待滚动条的父级div元素并设置滚动条位置
js = 'document.getElementsByClassName("game-store__block")[0].scrollTop={}'.format(top_length)
driver.execute_script(js)
print(top_length)
result = self.is_not_visible(driver, 'body > div > div.game-store__main > div > div.loading-wrapper')
if result:
break
else:
print('继续滚动')
print('滚动结束')
# 对象方法
def is_not_visible(self, driver, css_selector, timeout=1):
try:
WebDriverWait(driver, int(timeout)).until_not(EC.visibility_of_element_located(("css selector", css_selector)))
return True
except TimeoutException:
return False
11. 获取指定元素集合的个数
# 获取ul
ul = driver.find_element_by_css_selector('body > .frm_control_group > div > ul')
# 获取ul下面的所有li 注意是 elements
lis = ul.find_elements_by_tag_name('li')
print(len(lis))
10. 选择指定位置的li
# 点击触发下拉框
driver.find_element_by_css_selector('#dropdown_menu > a > i').click()
# 选中ul 这里也可以选外层的元素 最好是选li的直属父级
ul = driver.find_element_by_css_selector('body > .frm_control_group > div > ul')
# 选择指定索引位置的li (lin_index从0开始)
ul.find_elements_by_tag_name('li')[li_index].click()
# 获取ul下li的个数
li_num = len(ul.find_elements_by_tag_name('li'))
9. 鼠标移动到指定位置
from selenium.webdriver import ActionChains
ActionChains(driver).move_to_element(driver.find_element_by_id('#tt-link-ul')).perform()
8. 获取隐藏的元素
先判断元素是否被隐藏
element = driver.find_element_by_css_selector('#table-info > tbody > tr > td')
display = element.is_displayed()
如果 display 是 false 则表示元素被隐藏了
# 获取内部 html 字符串
element.get_attribute("innerHTML"))
# 获取文本内容
element.get_attribute('textContent')
7. 常用设置
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
bin_path = 'chromedriver驱动 所在的路径'
chrome_options = Options()
chrome_options.add_argument('--headless') # 无头浏览器,即看不到浏览器界面
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-zygote')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument(
'--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36')
driver = webdriver.Chrome(bin_path, chrome_options=chrome_options)
6. 等待元素出现 设置了最长等待15秒,超时不出现则抛出异常
from selenium.webdriver.support.ui import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
WebDriverWait(driver, 15).until(EC.visibility_of_element_located(("css selector", '#menuBar')))
5. 切换当前浏览器的标签页
driver.switch_to.window(driver.window_handles[1]) # 切换到浏览器的第二个标签页 0 是第一个
4. 选择器
driver.find_element_by_id('login_form') # 根据id,这个不需要加#号
driver.find_element_by_css_selector('#menuBar > li.weui-desktop >ul > li:nth-child(3) > a > span') # 根据css选择器
3. 设置窗口大小
driver.maximize_window() # 最大化
driver.set_window_size(1920, 1080) # 指定大小
2. selenium 添加 cookie
# driver = webdriver.Chrome(bin_path, chrome_options=chrome_options)
# 循环 cookie_list(自己构造) 取出cookie键值对组成的 dict
for cookie_dict in cookie_lists:
# cookie_dict 例如 {'name': 'PHPSESSID', 'value': 'asd21dvyt2cdyt2cdt12ytc21yc'}
# 字典类型,name 对应 cookie的键, value 对应值
driver.add_cookie(cookie_dict )
1. invalid cookie domain
爬取多类内容,第一次需要登陆,接着就保存cookie直接请求,给selenium直接添加cookie的时候报错 invalid cookie domain
原因:驱动打开浏览器,没有请求页面,直接添加对应cookie。
解决办法:驱动打开浏览器后 先访问cookie 所属网站,一般都是登录页,然后再次添加cookie,就可以了。
添加对应网站的cookie之前,要先访问一次网站页面。