用户工具

站点工具


python:selenium

3.6 selenium

1. 安装浏览器驱动

1.1 查浏览器版本

1.2 谷歌对应驱动

1.3 edge对应驱动

1.4 驱动配置

将下载的压缩包,放置到Chrome安装目录下

点击系统变量中的path,点击新增,并将chromeDriver的安装目复制填入后,点击确定。

1.5 python设置驱动路径

from selenium import webdriver  
executable_path=Service(r"C:/Program Files/Google/Chrome/Application/chromedriver.exe")
self.driver = webdriver.Chrome(service=executable_path)  # 当前浏览器驱动对象

2. 安装 Selenium

pip install selenium

2.1 更新

pip install –upgrade selenium

3. 函数

3.1 switch_to.frame()

4. 错误

4.1 unknown erro

unknown error: cannot find dict 'desiredCapabilities

成功解决成功解决 + 重启电脑

4.2 Selenium定位元素代码

'WebDriver' object has no attribute 'find_element_by_name'

from selenium.webdriver.common.by import By  #导入by

关于新版本selenium定位元素报错

4.3 phantomjs 不可用

module 'selenium.webdriver' has no attribute 'phantomjs'

卸载最新版本,安装2.48.0

Selenium 不再支持 PhantomJS 的解决办法

4.4 如何使用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/")

4.5 Chrome 浏览器窗口关闭太快

在使用 Selenium 操作 Chrome 浏览器时,有时 Chrome 浏览器窗口会在操作完成后立即关闭。这可能是因为代码中缺少类似 time.sleep(5) 这样的代码,导致程序无法等待操作完成后再关闭浏览器。

python/selenium.txt · 最后更改: 2024/08/01 07:52 由 争当好青年