基本使用
- 导入:
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)
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)
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)
- 获取网页代码:
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')