質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

解決済

1回答

1376閲覧

seleniumとPythonでChromeの拡張機能をインストールしたい。

NagaseTomohiko

総合スコア91

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

0クリップ

投稿2020/08/30 12:31

解決したいこと

 seleniumとPythonでChromeの拡張機能を自動でインストールしたいが、インストール手前のポップアップ表示の「拡張機能を追加」ボタンが押せません。どうすればできるようになるでしょうか。

試した環境

Windows10 Pro
Python 3.7.9 64bit版
chromedriver-binary 84.0.4147.30.0
selenium 3.141.0

試したこと

 以下に試したソースを記載します。インストール対象は「楽天ウェブ検索」です。

python

1import os 2import time 3# selenium 4from selenium import webdriver 5from selenium.webdriver.common.by import By 6from selenium.webdriver.common.alert import Alert 7# ないと起動しない 8import chromedriver_binary 9from selenium.webdriver.support.expected_conditions import alert_is_present 10import selenium.webdriver.support.expected_conditions as EC 11from selenium.webdriver.support.wait import WebDriverWait 12 13def getOptions(): 14 options = webdriver.ChromeOptions() 15 options.add_argument( '--user-data-dir=' + 'UserData' ) 16 options.add_argument( '--profile-directory='+ 'Default' ) 17 return options 18 19# 初回起動、楽天検索拡張機能をインストール 20driver = webdriver.Chrome( options=getOptions() ) 21driver.get(r'https://chrome.google.com/webstore/detail/%E6%A5%BD%E5%A4%A9%E3%82%A6%E3%82%A7%E3%83%96%E6%A4%9C%E7%B4%A2/iihkglbebihpaflfihhkfmpabjgdpnol?hl=ja') 22 23WebDriverWait(driver, 15).until( EC.element_to_be_clickable( (By.XPATH, '//div[@class="g-c-Hf"]') ) ) 24driver.find_element_by_xpath( '//div[@class="g-c-Hf"]' ).click() # Chromeに追加 ボタンクリック 25WebDriverWait(driver, 5).until(alert_is_present()) # Alert表示待ち 26 27Alert(driver=driver).accept() # 「拡張機能を追加」ボタン クリックできるはず… 28time.sleep(5) 29driver.close() 30driver.quit()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Python

1import os 2import time 3# selenium 4from selenium import webdriver 5from selenium.webdriver.common.by import By 6from selenium.webdriver.common.alert import Alert 7# ないと起動しない 8import chromedriver_binary 9from selenium.webdriver.support.expected_conditions import alert_is_present 10import selenium.webdriver.support.expected_conditions as EC 11from selenium.webdriver.support.wait import WebDriverWait 12from time import sleep 13import sys 14import ctypes 15import win32com.client 16 17def getOptions(): 18 options = webdriver.ChromeOptions() 19 options.add_argument( '--user-data-dir=' + 'UserData' ) 20 options.add_argument( '--profile-directory='+ 'Default' ) 21 return options 22 23# 初回起動、楽天検索拡張機能をインストール 24driver = webdriver.Chrome( options=getOptions() ) 25driver.get(r'https://chrome.google.com/webstore/detail/%E6%A5%BD%E5%A4%A9%E3%82%A6%E3%82%A7%E3%83%96%E6%A4%9C%E7%B4%A2/iihkglbebihpaflfihhkfmpabjgdpnol?hl=ja') 26 27WebDriverWait(driver, 15).until( EC.element_to_be_clickable( (By.XPATH, '//div[@class="g-c-Hf"]') ) ) 28driver.find_element_by_xpath( '//div[@class="g-c-Hf"]' ).click() # Chromeに追加 ボタンクリック 29 30html = driver.page_source 31 32handle = ctypes.windll.user32.FindWindowW( "Chrome_WidgetWin_1",0) 33ctypes.windll.user32.SetForegroundWindow(handle) 34 35Shell = win32com.client.Dispatch("WScript.Shell") 36for i in range(2): 37 Shell.SendKeys("{TAB}") 38 sleep (1) 39Shell.SendKeys("~") 40 41time.sleep(10) 42driver.close() 43driver.quit()

投稿2020/08/30 15:06

Reach

総合スコア733

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

NagaseTomohiko

2020/08/30 21:54

Thanks for the answer. In my environment, I changed range(2) to range(3) and it worked as expected. 回答ありがとうございます。私の環境では、range(2) を range(3)に変えたら、期待する動作をしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問