MENU

seleniumが急に動かないならProxyと証明書を疑って対策

seleniumが動かなくなった場合の対応-PROXYと証明書の問題

※当サイトはアフェリエイト広告を利用しています

今までseleniumを利用していたが急にseleniumが使えなくなったという場合はないでしょうか?

考えられるのはselenium3バージョンからselenium4に変更した時にコードの書き方が変わっていることもあるでしょう。

ただ、上記とは違い使えなくなる理由としてProxy問題と証明書の問題があります。この問題はseleniumが使えなくなった原因としてわかりにくいのでなぜ?できないかをずっと解決できない方が多いと思いましたので本記事で対策方法をご紹介いたします。

目次

selenium Proxy対策

ほとんどがこの問題だと思われます。

seleniumのエラー内容を見るとlocalhostの表示がどこかにありませんか?

対応方法は簡単で環境変数に下記をセットすることでproxyを回避できます。

項目名:no_proxy
  値:localhost

selenium 証明書対策

あまりないですが証明書パターンも考えておきます。この場合は下記のように書きます。

対象の環境変数を空文字にすることで証明書パターンも回避します。

import os

os.environ["CURL_CA_BUNDLE"] = ""

Chromeは自動テストソフトウェアによって制御されていますを消す

seleniumで自動操作する時にクロームブラウザのタブに「自動テストソフトウェアによって制御されています」が出る場合があります。

このままだと操作できない場合があるので下記コマンドでこの表示をださないようにします。

chrome_options=webdriver.ChromeOptions()
chrom_options.add_experimental_option("excudeSwitches",["enable-automation","load-extension"])

seleniumを動かしてみる

さて上記2つを実施して下記をinportしておけばseleniumが動くようになっています。動くseleniumのコードを記載しておきます。

import urllib3
import os
import sys

#クロームドライバー系
import chromedriver_binary
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service


#証明書対策
os.environ["CURL_CA_BUNDLE"] = ""

#chromedriver.exeのpath指定
driver_path = "chromedriver.exeのpath"

#ターゲットのURL
url = "https:www.yahoo.co.jp/"


service = Service(executable_path=driver_path)
chrome_options=webdriver.ChromeOptions()
chrome_options.add_experimental_option("excludeSwitches",["enable-automation","load-extension"])

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=chrome_options)

driver.get(url)

動きましたでしょうか?

指定したURLが開いていればOKです。

seleniumの使い方については過去のSeleniumのWebdriver自動更新とブラウザ操作のPythonコマンドでも触れておりますのでご興味があればご覧くださいませ。

最後に私がPythonの勉強をして入門者から中級者までにこの本1冊あれば習得できるおすすめ本記事も書いております【本1冊で】Python入門から中級までなれるおすすめ本ご興味がある方は御覧くださいませ。

seleniumが動かなくなった場合の対応-PROXYと証明書の問題

この記事が気に入ったら
フォローしてね!

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする

目次