====== 3.6 selenium ======
===== -# 安装浏览器驱动 =====
==== - 查浏览器版本 ====
==== - 谷歌对应驱动 ====
[[http://chromedriver.storage.googleapis.com/index.html|下载地址]]
[[https://getwebdriver.com/chromedriver|下载地址 版本123.125.126]]
==== - edge对应驱动 ====
[[https://msedgewebdriverstorage.z22.web.core.windows.net/?form=MA13LH|下载地址]]
[[https://blog.csdn.net/m0_61552056/article/details/134956649|安装 selenium 及配置 edge 浏览器驱动]]
==== - 驱动配置 ====
将下载的压缩包,放置到Chrome安装目录下
点击系统变量中的path,点击新增,并将chromeDriver的安装目复制填入后,点击确定。
==== - python设置驱动路径 ====
from selenium import webdriver
executable_path=Service(r"C:/Program Files/Google/Chrome/Application/chromedriver.exe")
self.driver = webdriver.Chrome(service=executable_path) # 当前浏览器驱动对象
===== -# 安装 Selenium =====
pip install selenium
==== - 更新 ====
pip install --upgrade selenium
===== -# 函数 =====
==== - switch_to.frame() ====
===== -# 错误 =====
==== - unknown erro ====
unknown error: cannot find dict 'desiredCapabilities
{{ :python:desiredcapabilities.png?400 |}}
[[https://blog.csdn.net/weixin_52024937/article/details/126273695|成功解决成功解决]] + 重启电脑
==== - Selenium定位元素代码 ====
'WebDriver' object has no attribute 'find_element_by_name'
from selenium.webdriver.common.by import By #导入by
[[https://blog.csdn.net/m0_49076971/article/details/126233151|关于新版本selenium定位元素报错]]
==== - phantomjs 不可用 ====
module 'selenium.webdriver' has no attribute 'phantomjs'
{{ :python:phantomjs.png?&400 |}}
卸载最新版本,安装2.48.0
{{ :python:not_callable.png?400 |}}
Selenium 不再支持 PhantomJS 的解决办法
==== - 如何使用Headless Chrome ====
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')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://cnblogs.com/")
==== - Chrome 浏览器窗口关闭太快 ====
在使用 Selenium 操作 Chrome 浏览器时,有时 Chrome 浏览器窗口会在操作完成后立即关闭。这可能是因为代码中缺少类似 time.sleep(5) 这样的代码,导致程序无法等待操作完成后再关闭浏览器。