【问题标题】:How can I use a Google Chrome extension with Selenium?如何在 Selenium 中使用 Google Chrome 扩展程序?
【发布时间】:2016-07-22 17:57:42
【问题描述】:

我正在尝试从类似这样的页面中抓取匹配信息(页面格式相同,但对于不同的匹配显然具有不同的值):https://csgolounge.com/match?m=8967

问题是,我想要的信息只有在您使用 Chrome 扩展程序“Lounge Destroyer”时才会显示......经过大量的试验和错误,我终于想通了,为了获得这些信息,我使用的python脚本必须以某种方式将该扩展名“包含在其中”。我在这里浏览了其他答案,并从另一个 stackoverflow 线程中找到了这段代码,该线程演示了如何在使用 selenium 时添加扩展:

            from selenium import webdriver
            from selenium.webdriver.chrome.options import Options

            chop = webdriver.ChromeOptions()
            chop.add_extension('Adblock-Plus_v1.4.1.crx')
            driver = webdriver.Chrome(chrome_options = chop)

我去Chrome Extension Downloader为LoungeDestroyer获取.crx文件,将它放在chrome扩展文件夹中(从“获取信息”中获取文件地址),并为我的目的稍微修改了上面的代码以获得以下:

            from selenium import webdriver
            from selenium.webdriver.chrome.options import Options

            chop = webdriver.ChromeOptions()
            chop.add_extension('Users/Username_Here/Library/Application Support/Google/Chrome/Default/Extensions/ghahcnmfjfckcedfajbhekgknjdplfcl/LoungeDestroyer_v0.9.3.7.crx')
            driver = webdriver.Chrome(chrome_options = chop)

            matchID = raw_input("Enter match ID (four digit number in CSGL URL): ")
            driver.get("https://csgolounge.com/match?m="+matchID)

问题是,我认为我没有替换原始代码中的“Adblock-Plus_v1.4.1.crx”是正确的。

运行我修改后的版本会返回以下错误:

            IOError: Path to the extension doesn't exist

非常感谢任何帮助或建议。

【问题讨论】:

    标签: python google-chrome selenium google-chrome-extension web-scraping


    【解决方案1】:

    问题是我没有安装 chromedriver (http://chromedriver.storage.googleapis.com/index.html?path=2.21/)。安装后,我必须在我的代码中输入 chromedriver 可执行文件的路径。总而言之,这是有效的代码:

                from selenium import webdriver
                from selenium.webdriver.chrome.options import Options
    
    
                chop = webdriver.ChromeOptions()
                chop.add_extension('/Users/Username_Here/Library/Application Support/Google/Chrome/Default/Extensions/ghahcnmfjfckcedfajbhekgknjdplfcl/LoungeDestroyer_v0.9.3.7.crx')
                driver = webdriver.Chrome(executable_path='/Users/Username_Here/Downloads/chromedriver', chrome_options = chop)
    
                # go to the match page
                matchID = raw_input("Enter match ID (four digit number in CSGL URL): ")
                driver.get("https://csgolounge.com/match?m="+matchID)
    

    另外,我收到扩展路径错误的原因是我在文件地址中的“用户”一词前面没有正斜杠。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      相关资源
      最近更新 更多