selenium 备忘
selenium 备忘
本地chrome、无头模式、新标签页、滚动、iframe、shadow dom
import time
import subprocess
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
browser_path = r"C:/Program Files/Google/Chrome/Application/chrome.exe"
chrome_file_path = r'D:\data\chrome' # chrome 用户数据存储位置
def start_controlled_mode():
"""
受控模式,会被chrome识别为被自动化测试工具控制
"""
driver = webdriver.Chrome()
driver.implicitly_wait(5) # 隐式等待 5s
driver.get(f'https://www.cnblogs.com/aaron-agu')
ele = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "profile_block"))
) # 等待id为profile_block的dom加载完成,超时时间为10s
name = ele.find_element(By.TAG_NAME, "a").text
print(name)
time.sleep(20)
driver.close()
def start_local_mode():
"""
本地模式,与用户打开的chrome一样
"""
# subprocess.Popen(f'{browser_path} --remote-debugging-port=9527 --user-data-dir="{chrome_file_path}"'
# f' --proxy-server="http://proxy_ip:proxy_port"') # 使用代理打开chrome
subprocess.Popen(f'{browser_path} --remote-debugging-port=9527 --user-data-dir="{chrome_file_path}"')
options = webdriver.ChromeOptions() # 创建一个配置对象
options.add_experimental_option("debuggerAddress", "127.0.0.1:9527") # 使用已经打开的chrome,即监听9527端口的chrome。
options.page_load_strategy = "eager" # 等待 DOM(文档对象模型)加载完成(即 DOMContentLoaded 事件触发后),但不等待所有资源(如图片、样式表等)加载完成
driver = webdriver.Chrome(options=options)
driver.set_page_load_timeout(20)
driver.implicitly_wait(5) # 隐式等待 5s
driver.get(f'https://www.cnblogs.com/aaron-agu')
ele = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "profile_block"))
) # 等待id为profile_block的dom加载完成,超时时间为10s
name = ele.find_element(By.TAG_NAME, "a").text
print(name)
time.sleep(20)
driver.close()
def start_headless_mode():
"""
无头模式
"""
# 创建 ChromeOptions 对象
options = Options()
# 启用无头模式
options.add_argument("--headless=new") # Chrome 109 及以上版本使用 "new"
# options.add_argument("--headless") # Chrome 108 及以下版本
# 禁用图片加载
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_argument("--blink-settings=imagesEnabled=false") # 禁用图片加载
# 禁用js
options.add_argument("--disable-javascript")
# 页面加载策略
options.page_load_strategy = "eager" # eager:等待dom加载完成; none:什么都不等待;
# 启动 Chrome 浏览器
driver = webdriver.Chrome(options=options)
driver.get("https://www.cnblogs.com/aaron-agu")
# 打印页面标题
print(driver.title)
ele = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "profile_block"))
) # 等待id为profile_block的dom加载完成,超时时间为10s
name = ele.find_element(By.TAG_NAME, "a").text
print(name)
# 关闭浏览器
driver.quit()
def new_table():
"""
新标签页
"""
options = Options()
options.page_load_strategy = "eager"
driver = webdriver.Chrome(options=options)
driver.get("https://www.cnblogs.com/aaron-agu")
# 新标签页打开一个url
driver.execute_script(f"window.open('https://www.cnblogs.com/aaron-agu/p/18029935', 'new_window')")
driver.switch_to.window(driver.window_handles[-1]) # 切换到新标签页 iframe 也需要switch_to
def scroll():
"""
页面滚动
"""
options = Options()
options.page_load_strategy = "eager"
driver = webdriver.Chrome(options=options)
driver.get("https://www.cnblogs.com/aaron-agu")
# 向下滚动 1000 像素
driver.execute_script("window.scrollBy(0, 1000);")
time.sleep(1)
# 向上滚动 500 像素
driver.execute_script("window.scrollBy(0, -1000);")
time.sleep(1)
# 找到目标元素
element = driver.find_elements(By.CSS_SELECTOR, "div.day")[5]
# 滚动到目标元素
driver.execute_script("arguments[0].scrollIntoView();", element)
time.sleep(1)
# 模拟鼠标滚轮滚动
# 创建 ActionChains 对象
actions = ActionChains(driver)
# 模拟鼠标滚轮向下滚动
actions.send_keys(Keys.PAGE_DOWN).perform()
time.sleep(1)
# 模拟鼠标滚轮向上滚动
actions.send_keys(Keys.PAGE_UP).perform()
time.sleep(1)
# 滚动到页面底部
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1)
# 滚动到页面顶部
driver.execute_script("window.scrollTo(0, 0);")
time.sleep(1)
driver.close()
def shadow_dom():
options = Options()
driver = webdriver.Chrome(options=options)
driver.get("https://developer.salesforce.com/docs/atlas.en-us.netzero_cloud_dev_guide.meta/"
"netzero_cloud_dev_guide/sforce_api_objects_airtravelemssnfctr.htm#maincontent")
main_content = driver.find_element(By.CSS_SELECTOR, "main[id='maincontent'] doc-xml-content")
main_content_shadow_root = main_content.shadow_root
doc_content = main_content_shadow_root.find_element(By.CSS_SELECTOR, "doc-content")
doc_content_shadow_root = doc_content.shadow_root
table = doc_content_shadow_root.find_element(By.CSS_SELECTOR, ".featureTable.sort_table")
print(table.tag_name)
if __name__ == "__main__":
# start_controlled_mode()
# start_local_mode()
# start_headless_mode()
# scroll()
shadow_dom()
文章出处:http://www.cnblogs.com/aaron-agu/
只有毅力和决心才能使人真正具有价值!

浙公网安备 33010602011771号