前提
Python 3.9.7 (anaconda3使用)でwebから要素をとってくるプログラムを作っています。
以下のエラーメッセージが発生しました。
不慣れなので必要な情報が抜けているかもしれません。
実現したいこと
以下のサイトから、マップ名(devツールで見てみると 'map-title'のクラスがついています)と、ローテーション時間(同様に 'map-countdown'のクラスがついています)をテキストとして取り出したいです。
発生している問題・エラーメッセージ
どこからが必要な情報かがわからないのでターミナルをコピペしました。
見ずらくて申し訳ないです。
INUDOGはユーザー名です(笑)
(py39) C:\Users\INUDOG\OneDrive\ドキュメント\my python projects>C:/Users/INUDOG/anaconda3/envs/py39/python.exe "c:/Users/INUDOG/OneDrive/ド キュメント/my python projects/test.py" c:\Users\INUDOG\OneDrive\ドキュメント\my python projects\test.py:10: DeprecationWarning: use options instead of chrome_options driver = webdriver.Chrome('C:/Program Files (x86)/selenium/chromedriver',chrome_options=options) DevTools listening on ws://127.0.0.1:59180/devtools/browser/2d328c73-125b-440e-ac5a-6fb866e6a794 Traceback (most recent call last): File "c:\Users\INUDOG\OneDrive\ドキュメント\my python projects\test.py", line 13, in <module> map = driver.find_element_by_xpath('//*[@id="app-shell"]/div[2]/app-br-map-rotation/app-br-rotation/app-map-rotation/div/app-hero/div/div[2]') File "C:\Users\INUDOG\anaconda3\envs\py39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) File "C:\Users\INUDOG\anaconda3\envs\py39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\Users\INUDOG\anaconda3\envs\py39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\INUDOG\anaconda3\envs\py39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="app-shell"]/div[2]/app-br-map-rotation/app-br-rotation/app-map-rotation/div/app-hero/div/div[2]"} (Session info: headless chrome=101.0.4951.67)
該当のソースコード
python
1from lib2to3.pgen2 import driver 2from webbrowser import Chrome 3from selenium import webdriver 4from selenium.webdriver.common.keys import Keys 5from selenium.webdriver.chrome.options import Options 6 7options = Options() 8options.add_argument('--headless') 9driver = webdriver.Chrome('C:/Program Files (x86)/selenium/chromedriver',chrome_options=options) 10driver.get('https://apexmap.kuroi.io/br') 11 12map = driver.find_element_by_xpath('//*[@id="app-shell"]/div[2]/app-br-map-rotation/app-br-rotation/app-map-rotation/div/app-hero/div/div[2]') 13countdown = driver.find_element_by_xpath('//*[@id="app-shell"]/div[2]/app-br-map-rotation/app-br-rotation/app-map-rotation/div/app-hero/div/div[2]/div[1]') 14 15print(map + countdown)
試したこと
最初はクラス名で探していたのですが、見つからないとエラーが返ってきたのでxpathでやってみましたができませんでした。
headlessオプションでは未確認ですが、headlessオプション無し時に10行目までは正しく動作したのを確認しています。(目的のページを開けたということ)
補足情報(FW/ツールのバージョンなど)
本題に関係するかわからないのですが、上記のリンクはそのリンク先に飛ぶと、末尾に 'br' が追加されhttps://apexmap.kuroi.io/brとなります。
なぜこのようなことが起きるのですか?
回答2件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/05/24 02:57