python - 如何使用 Selenium 和 Python 安装 Chrome 扩展

标签 python selenium google-chrome google-chrome-extension selenium-chromedriver

您好,我正在尝试使用 python 安装带有 Selenium 的 Chrome 扩展,我尝试使用 ChromeDriver - WebDriver for Chrome

但它不起作用,这是我的代码:

from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import ChromeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import presence_of_element_located

import re  # regular expressions, are imported from python directly
import time
import numpy as np
import pandas as pd
import functions_database

# Pandas read CSV
df_read = pd.read_csv(
    '/home/daniel/amazon-project-scrapers/ss_scraper.edited2.csv')

amazon_data = list(df_read.amz_search)

# Chrome Driver + install plugin
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/home/daniel/amazon-project-scrapers/chromedriver_linux64/DS-Amazon-Quick-View_v2.8.9.crx"));
ChromeDriver driver = new ChromeDriver(options);

driver = webdriver.Chrome(executable_path='/home/daniel/amazon-project-scrapers/chromedriver_linux64/chromedriver')
driver.get('https://www.amazon.com/')

这是我遇到的错误:

File "camel_scraper.py", line 23
    ChromeOptions options = new ChromeOptions();
                        ^
SyntaxError: invalid syntax

我尝试用其他 3 种不同的方式来做到这一点,实际上 Stack Overflow 中有一个类似的问题,其答案已被弃用,如果我再次找到它,我会将链接写在这里。

最佳答案

使用 Selenium 添加/安装 DS-Amazon-Quick-View Chrome 扩展程序的客户端您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_extension('/home/daniel/amazon-project-scrapers/chromedriver_linux64/DS-Amazon-Quick-View_v2.8.9.crx')
    driver = webdriver.Chrome(options=chrome_options, executable_path='/path/to/chromedriver')
    driver.get('https://www.google.co.in')
    
<小时/>

引用

您可以在以下位置找到一些相关讨论:

关于python - 如何使用 Selenium 和 Python 安装 Chrome 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59954995/

相关文章:

javascript - 更改 "input"事件处理程序内的输入值会阻止 Chrome 和 IE 中触发 "change"事件(但在 Firefox 中不会)

css - 有没有支持分页的浏览器?

java - 日志级别的数值有哪些用例?

python - 如何添加 Facebook 用户登录功能

javascript - 用 Protractor 打开文件

python - 不安全的浏览

selenium - 如何在下拉列表中滚动到隐藏元素?

javascript - 如何暂时暂停浏览器渲染然后恢复整个页面

python - Keras 中的 "metrics"是什么?

python - formencode无效的返回类型