【问题标题】:Error while installing chrome extension through Python - selenium通过 Python 安装 chrome 扩展时出错 - selenium
【发布时间】:2017-06-08 23:30:00
【问题描述】:

我正在尝试使用 Python Selenium 安装 chrome 扩展。 当我单击添加到 chrome 按钮时,会生成一个弹出窗口(不知道它是否是 java 脚本),询问:“添加扩展名”、“取消”。我想点击“添加扩展”,但出现以下错误:

selenium.common.exceptions.NoAlertPresentException: Message: no alert open

我的代码:

from selenium import webdriver
import time

driver=webdriver.Chrome()
driver.implicitly_wait(30)
driver.get("https://chrome.google.com/webstore/detail/buyhatke/jaehkpjddfdgiiefcnhahapilbejohhj?hl=en")
time.sleep(15)
element=driver.find_element_by_css_selector("body > div.F-ia-k.S-ph.S-Rc-qa > div.h-F-f-k.F-f-k > div > div > div.e-f-o > div.h-e-f-Ra-c.e-f-oh-Md-zb-k > 
div.dd-Va.g-c-wb.g-eg-ua-Uc-c-za.g-c-Oc-td-jb-oa.g-c")
element.click()
alert = driver.switch_to.alert
alert.accept() 

帮我安装。

更新代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import os

executable_path = "C:\\Users\\SACHIN\\AppData\\Local\\Programs\\Python\\Python36\\chromedriver"
os.environ["webdriver.chrome.driver"] = executable_path

chrome_options = Options()
chrome_options.add_extension("C:\\Users\\SACHIN\\AppData\\Local\\Google\\chrome\\User Data\\Default\\Extensions\\jaehkpjddfdgiiefcnhahapilbejohhj\\
3.4.143_0")

driver = webdriver.Chrome(executable_path=executable_path,chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    这是因为您尝试使用 switch_to.alert 选择的下载选项弹出实际上并不是 JS 警报弹出。您不能选择使用 Selenium 的 switch_to 方法。您需要使用DesiredCapabilities 类来选择此选项,然后在您的代码中使用它。

    根据 ChromeDriver 文档,给定here,请使用打包的(带有.crx 扩展名的那个)或解压缩的扩展文件(包含扩展名的目录,包括manifest.json file)并使用DesiredCapabilities 加载它.这可以使用

         from selenium.webdriver.chrome.options import Options
    
         executable_path = "path_to_webdriver"
         os.environ["webdriver.chrome.driver"] = executable_path
    
         chrome_options = Options()
         chrome_options.add_extension('path_to_extension')
    
         driver = webdriver.Chrome(executable_path=executable_path,chrome_options=chrome_options)
         driver.get("http://stackoverflow.com")
         driver.quit()
    

    【讨论】:

    • 你能帮我在哪里找到扩展路径
    • path to extension 是您下载.crx 文件的路径。假设你在一个 linux 系统上,它可能是 /Users/Downloads 或直接的一些
    • 我使用的是 Windows 10
    • 我设法找到了扩展路径。我现在收到以下错误 -> Traceback(最近一次调用最后一次):文件“C:/Users/SACHIN/AppData/Local/Programs/Python/Python36/stacktry.py”,第 12 行,在 driver = webdriver .Chrome(executable_path=executable_path,chrome_options=chrome_options) PermissionError: [Errno 13] Permission denied: 'C:\\Users\\SACHIN\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\扩展名\\jaehkpjddfdgiiefcnhahapilbejohhj\\3.4.143_0'
    猜你喜欢
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    相关资源
    最近更新 更多