阅读量:0
基本使用
- 导入:
from selenium import webdriver
- 创建浏览器操作对象:
browser = webdriver.Chrome()
- 访问网站
# 访问网站 url = 'https://www.jd.com' browser.get(url)
""" selenium基本使用 Author:binxin Date:2023/11/29 14:42 """ # 导入 from selenium import webdriver # 创建浏览器操作对象 browser = webdriver.Chrome() # 访问网站 url = 'https://www.jd.com' browser.get(url) # page_source获取网页源码 content = browser.page_source print(content)
元素定位
- 导入:
from selenium.webdriver.common.by import By
inputTag = driver.find_element(By.ID, "value") inputTag = driver.find_element(By.CLASS_NAME, "value") inputTag = driver.find_element(By.NAME, "value") inputTag = driver.find_element(By.TAG_NAME, "value") inputTag = driver.find_element(By.XPATH, "value") inputTag = driver.find_element(By.CSS_SELECTOR, "value")
""" selenium 元素定位 Author:binxin Date:2023/11/29 15:05 """ from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() url = 'https://www.baidu.com' browser.get(url) # 元素定位 # 根据ID找到对象 # button = browser.find_element(By.ID,'su') # 根据标签属性的属性值获取对象 # button = browser.find_element(By.NAME, 'wd') # 根据xpath语句获取对象 # button = browser.find_element(By.XPATH, '//input[@id="su"]') # 根据标签名获取对象 # button = browser.find_element(By.TAG_NAME, 'input') # 使用bs4的语法获取对象 # button = browser.find_element(By.CSS_SELECTOR, '#su') button = browser.find_element(By.LINK_TEXT, '新闻') print(button)
元素信息
- 获取元素属性:
.get_attribute('class')
- 获取元素文本:
.txt
- 获取标签名:
.tag_name
""" 元素信息 Author:binxin Date:2023/11/29 16:31 """ from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() url = 'https://www.baidu.com' browser.get(url) input = browser.find_element(By.ID, 'su') # 获取标签的属性 print(input.get_attribute('class')) # 获取标签名 print(input.tag_name) a = browser.find_element(By.LINK_TEXT, '新闻') # 获取元素文本 print(a.text)
交互
- 点击:
click()
- 输入:
send_keys()
- 后退操作:
browser.back()
- 前进操作:
browser.forward()
- 模拟JS滚动:
js='document.documentElement.scrollTop=100000' browser.execute_script(js) # 执行js代码
- 获取网页代码:
page_source
- 退出:
browser.quit()
""" selenium交互 Author:binxin Date:2023/11/29 16:47 """ from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() url = 'https://www.baidu.com' browser.get(url) import time time.sleep(2) # 获取文本框对象 input = browser.find_element(By.ID, 'kw') # 在文本框中输入周杰伦 input.send_keys('周杰伦') time.sleep(2) # 获取百度一下按钮 button = browser.find_element(By.ID, 'su') # 点击按钮 button.click() time.sleep(2) # 滑动窗口到底部 js_bottom = 'document.documentElement.scrollTop=100000' browser.execute_script(js_bottom) time.sleep(2) # 获取下一页按钮 next = browser.find_element(By.XPATH, '//a[@class="n"]') # 点击下一页 next.click() time.sleep(2) # 返回上一页 browser.back() time.sleep(2) # 重新回到上一页 browser.forward() time.sleep(3) # 退出 browser.quit()
Chrome handless
- 基本代码
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') browser = webdriver.Chrome(options=chrome_options)
- 封装配置
from selenium import webdriver from selenium.webdriver.chrome.options import Options def share_browser(): chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') browser = webdriver.Chrome(options=chrome_options) return browser
""" Chrome handless Author:binxin Date:2023/11/30 11:06 """ from selenium import webdriver from selenium.webdriver.chrome.options import Options def share_browser(): chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') browser = webdriver.Chrome(options=chrome_options) return browser # 封装调用 browser = share_browser() url = 'http://www.baidu.com/' browser.get(url) browser.save_screenshot('baidu.png')