selenium常用操作
from selenium import webdriver
# 解决找不到chrome浏览器对象的问题
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = 'chrome的路径'
browser = webdriver.Chrome('./chromedriver.exe', options=options) # 将驱动放在脚本所在的文件夹 browser.get('https://www.baidu.com')
# 实例化浏览器对象: from selenium import webdriver browser = webdriver.Chrome('driverpath')
# 发送get请求: browser.get('https://www.baidu.com') # 获取页面元素: find_element_by_id:根据元素的id find_element_by_name:根据元素的name属性 find_element_by_xpath:根据xpath表达式 find_element_by_class_name:根据class的值 find_element_by_css_selector:根据css选择器
# 节点交互操作: click(): 点击 send_keys(): 输入内容 clear(): 清空操作 execute_script(js): 执行指定的js代码
# JS代码: window.scrollTo(0, document.body.scrollHeight)可以模拟鼠标滚动一屏高度
# js代码:window.stop() 停止加载
quit(): 退出浏览器
# 获取网页的数据: browser.page_source ---> str类型,可以直接用解析库进行解析
# frame 子集页面跳转 switch_to.frame('frameid')
# 跳回父级页面
switch_to.default_content()