Pythonのglobal変数に要素のデータが入っていない?
解決したいこと
Python selenium chromeの組み合わせでWEB画面の操作を試みています。
global変数「driver_global」に最新のdriverを格納しているつもりですがエラーが発生します。
引数としてdriverを渡すと上手く動くのですがglobal変数として扱いたいです。
恐らく記述ルールの認識誤りだと思うのですがご指摘いただきたいです。
発生している問題・エラー
name 'driver_global' is not defined
例)
import subprocess
import time
import chromedriver_binary
import re
import os
import platform
import requests
from selenium.webdriver.common.by import By
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
global driver_global
global urlSBI_login
urlSBI_login = "https://www.sbifxt.co.jp/login.html"
def killChromeProcess():
print("stop_all_chrome_prcess")
cmd = 'taskkill /im chrome.exe /f'
returncode = subprocess.call(cmd)
print(returncode)
# def unlockSBI(driver):
def unlockSBI():
#このやりかただとうまくいく
#def unlockSBI(driver):
try:
# ロックボタン押下
driver=driver_global
button = driver.find_element(By.XPATH,'//*[@id="tradeTwoWay"]/section/form/div[1]/input[3]')
#このやりかただとうまくいく
#button = driver.find_element(By.XPATH,'//*[@id="tradeTwoWay"]/section/form/div[1]/input[3]')
button.click()
# エラーが発生した時はエラーメッセージを吐き出す。
except Exception as e:
print(e)
print("unlockSBI()のエクセプトに入った。エラーが発生しました。")
# 最後にドライバーを終了する
finally:
print("unlockSBI()のふぁいなりーにはいっておしまい")
def openSBI():
try:
# ドライバーを自動でインストールしてくれる
# Selenium3の場合
#driver = webdriver.Chrome(ChromeDriverManager().install())
# Selenium4の場合
# driver = webdriver.Chrome(executable_path='C:\fukui-mini共有\selenium\chromedriver_win32\chromedriver.exe')#←すでに非推奨になった書き方
# driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# ------- 追加部分 -------
# 最大の読み込み時間を設定 今回は最大30秒待機できるようにする
wait = WebDriverWait(driver=driver, timeout=30)
# -------------------------*/
# 開くURL
# ChromeDriverの呼び出し
# driver = webdriver.Chrome()
# utlを開く
driver.get(urlSBI_login)
# 要素が全て検出できるまで待機する
wait.until(EC.presence_of_all_elements_located)
# //<input type="text" name="ID" id="loginid" class="fz12">
name = driver.find_element(By.ID,"loginid")
name.send_keys("1234567890")
# //<input type="password" name="PASS" id="password" class="fz12">
name = driver.find_element(By.ID,"password")
name.send_keys("pw1234567890")
# <a href="javascript:postLoginUrl(MPAGE_URL);" onclick="$(this).click(function(e){ return false });">ログイン</a>
#リンクテキスト名が"画像"の要素を取得
button = driver.find_element(By.LINK_TEXT, "ログイン")
# element.submit
button.click()
# 要素が全て検出できるまで待機する
wait.until(EC.presence_of_all_elements_located)
# <button class="channel_open c-btn-trade p-btn-fx is-acOpend"><span class="c-lbl-sup">FX取引スタート</span></button>
# //*[@id="home_depositGraph"]/section[1]/div/div/ul/li[1]/p/button[1]/span
button = driver.find_element(By.CLASS_NAME,"c-lbl-sup")
button.click()
# # 要素が全て検出できるまで待機する
wait.until(EC.presence_of_all_elements_located)
#このやりかただとエラーになる
driver_global=driver
unlockSBI()#ロックを解除
#このやりかただと期待通りに動作する
#unlockSBI(driver)#ロックを解除
# エラーが発生した時はエラーメッセージを吐き出す。
except Exception as e:
print(e)
print("openSBI()のエクセプトに入った。エラーが発生しました。")
# 最後にドライバーを終了する
finally:
# Chromeを閉じる
# driver.close()
# driver.quit()
print("openSBI()のふぁいなりーにはいっておしまい")
#コマンドからインストールされているChromeのヴァージョンを取得
def get_chrome_version(cmd):
pattern = r'\d+\.\d+\.\d+'
stdout = os.popen(cmd).read()
version = re.search(pattern, stdout)
chrome_version = version.group(0)
print('Chrome Version : ' + chrome_version)
return chrome_version
#Chromeのヴァージョンから、適合する最新のChromeDriverのヴァージョンを取得
def get_chrome_driver_version(chrome_version):
url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE_' + chrome_version
response = requests.request('GET', url)
print('ChromeDriver Version : ' + response.text)
return response.text
# SBIのchromeが起動できているかチェック 0存在せず、1正常、
def chromeOpenCheckSBI():
try:
while True:
print("checkStatusSBI()でエラー")
# if IeOpenCheck()=1 then//chromeが起動していれば
# エラーが発生した時はエラーメッセージを吐き出す。
except Exception as e:
print(e)
print("checkStatusSBI()でエラー")
# 最後にドライバーを終了する
finally:
# Chromeを閉じる
# driver.close()
# driver.quit()
print("chromeOpenCheckSBI()のふぁいなりーにはいっておしまい")
#残っているといやなのですべてのchromeプロセスを落としておく
killChromeProcess()
#OSを判別
#cmd:それぞれのOSでChrome(Beta)のヴァージョンを確認するコマンド
#location:Chrome(Beta)の場所
pf = platform.system()
if pf == 'Windows':
print('OS : Windows')
cmd = r'reg query "HKEY_CURRENT_USER\Software\Google\Chrome Beta\BLBeacon" /v version'
location = 'C:/Program Files/Google/Chrome Beta/Application/chrome.exe'
elif pf == 'Darwin':
print('OS : Mac')
cmd = r'/Applications/Google\ Chrome\ Beta.app/Contents/MacOS/Google\ Chrome\ Beta --version'
location = '/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta'
if __name__ == '__main__':
#Chromeのヴァージョン取得
chrome_version = get_chrome_version(cmd)
#Chromeのヴァージョンから、適合する最新のChromeDriverのヴァージョンを取得
driver_version = get_chrome_driver_version(chrome_version)
#Chromeの場所を指定
options = Options()
options.binary_location = location
#ChromeDriverのヴァージョンを指定
chrome_service = ChromeService(ChromeDriverManager(version=driver_version).install())
driver = webdriver.Chrome(service=chrome_service,options=options)
driver.close()#インストール用に起動したchromeはこのあと使わないので閉じる
driver.quit()#インストール用に起動したchromeはこのあと使わないので閉じる
with ThreadPoolExecutor(max_workers=8) as executor:
executor.submit(openSBI())#初期処理
0 likes