2017-06-05 22 views
0

Python Seleniumを使用してchrome拡張機能をインストールしようとしています。 [クロムボタンに追加]をクリックすると、ポップアップ(Javaスクリプトかどうかわからない)が表示されます。「拡張子を追加する」、「キャンセル」。それをインストールするために私を助けPython-Seleniumでchrome拡張機能をインストールする際のエラー

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() 

:私は「拡張子を追加」をクリックしますが、私は次のようなエラーになっています:

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

は私のコードを。

更新コード:

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() 

答えて

0

あなたがswitch_to.alertを使用して選択しようとしていることをダウンロードオプションのポップアップが実際にJS警告ポップアップではないためです。 Seleniumのswitch_toメソッドを使用して選択することはできません。このオプションを選択してコード内で使用するには、DesiredCapabilitiesクラスを使用する必要があります。 hereを与えられたChromeDriverのドキュメントを1として

は、パック( .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() 
+0

を使用して行うことができ –

+0

拡張へのパスは、あなたの '.crx'ファイルをダウンロードしますパスです。あなたがLinuxシステムにいると仮定すると、それは/ Users/Downloadsまたはいくつかの直接かもしれません。 – demouser123

+0

私は窓を使用しています。 –

関連する問題