seleniumのバージョンアップに伴い find_element_byの変更がうまく行きません
Q&A
Closed
解決したいこと
ホームページ上でID、PASSを自動入力するプログラムがうまく動きません
seleniumのバージョンアップに伴い
find_element_by
が使えなくなったとのことで
find_element_by_xpath('
を
find_element(By.XPATH,(
に変更
他は問題なく動くのですが
find_element_by_id("input-21").send_keys("00" + id_num)
これをどう変更したら良いのかがわかりません
発生している問題・エラー
Traceback (most recent call last):
File "/home/omatsu/anaconda3/bin/2024/???.py", line 93, in reserve
log_in(driver, id_num, password, 2)
File "/home/???/anaconda3/bin/2024/???.py", line 56, in log_in
driver.find_element(By.XPATH,('//*[@id="main"]/div[1]/div[2]/div[1]/span[1]/a/span/div/span/span')).click()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/???/anaconda3/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 770, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/???/anaconda3/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 384, in execute
self.error_handler.check_response(response)
File "/home/???/anaconda3/lib/python3.12/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=131.0.6778.85)
Stacktrace:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/???/anaconda3/bin/2024/???.py", line 239, in
reservation_facility()
File "/home/???/anaconda3/bin/2024/???.py", line 226, in reservation_facility
result, error = reserve(driver, id_num, d, password="???")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/???/anaconda3/bin/2024/???.py", line 97, in reserve
driver.get('https://???/')
File "/home/???/anaconda3/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 393, in get
self.execute(Command.GET, {"url": url})
File "/home/???/anaconda3/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 384, in execute
self.error_handler.check_response(response)
File "/home/???/anaconda3/lib/python3.12/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=131.0.6778.85)
Stacktrace:
例)
NameError (uninitialized constant World)
または、問題・エラーが起きている画像をここにドラッグアンドドロップ
該当するソースコード
import sys, os
import json
import time
import datetime
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
def log_in(driver, id_num, password, sleep_time):
time.sleep(2)
driver.find_element(By.XPATH,('//[@id="main"]/div[1]/div[2]/div[1]/span[1]/a/span/div/span/span')).click()
time.sleep(2)
driver.find_element(By.ID,("input-21").Send_keys("00" + id_num))
driver.find_element(By.ID,("input-25").Send_keys(password))
while len(driver.find_elements(By.XPATH,('//[@id="fixed-btn-container-3"]/div[1]/div/button'))) != 0 :
driver.find_elements(By.XPATH,('//*[@id="fixed-btn-container-3"]/div[1]/div/button'))[0].click()
自分で試したこと
find_element_by_id("input-21").send_keys("00" + id_num)
これを
find_element(By.ID,("input-21").Send_keys,("00" + id_num))
こう変更しても駄目
find_element(By.ID,("input-21").send_keys("00" + id_num))
これも駄目
By.ID,("input-21").send_keys("00" + id_num)
ここの大文字・小文字、ドット、カンマの正解がわかりません
ド素人です
よろしくお願いいたします