pythonで下記のサイトのゲームをプレイするプログラムをseleniumで作っていたのですが、
エラーが解決できないので質問させていただきました。
サイト→http://orteil.dashnet.org/experiments/cookie/
クッキーをクリックしてアイテムを買うだけのゲームです。
プログラムの内容は
(1) 5秒間クッキーをクリックし続ける。
(2) その後、溜まったポイントで買えるだけアイテムを高価な順に買う。
また(1)に戻る。
この動作を5分間繰り返したいです。
エラー内容は
price_tag = driver.find_element(By.CSS_SELECTOR, f"#{store} b").text
の部分で
Traceback (most recent call last):
File "/home/usrname/programming/python/day-48/cookies_bot.py", line 28, in <module>
price_tag = driver.find_element(By.CSS_SELECTOR, f"#{store} b").text
File "/home/usrname/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 84, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "/home/usrname/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 396, in _execute
return self._parent.execute(command, params)
File "/home/usrname/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in execute
self.error_handler.check_response(response)
File "/home/usrname/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=105.0.5195.125)
Stacktrace:
#0 0x55c9fba73693 <unknown>
#1 0x55c9fb86cb0a <unknown>
#2 0x55c9fb86fb57 <unknown>
#3 0x55c9fb86f9ff <unknown>
#4 0x55c9fb86fcbc <unknown>
#5 0x55c9fb89feae <unknown>
#6 0x55c9fb8c2922 <unknown>
#7 0x55c9fb899d53 <unknown>
#8 0x55c9fb8c2a8e <unknown>
#9 0x55c9fb8d64b0 <unknown>
#10 0x55c9fb8c2743 <unknown>
#11 0x55c9fb898533 <unknown>
#12 0x55c9fb899715 <unknown>
#13 0x55c9fbac37bd <unknown>
#14 0x55c9fbac6bf9 <unknown>
#15 0x55c9fbaa8f2e <unknown>
#16 0x55c9fbac79b3 <unknown>
#17 0x55c9fba9ce4f <unknown>
#18 0x55c9fbae6ea8 <unknown>
#19 0x55c9fbae7052 <unknown>
#20 0x55c9fbb0171f <unknown>
#21 0x7f3fb21e3b43 <unknown>
と出ます。
10秒ほどは動いているのですが途中で上のエラーが出て止まってしまいます。
よろしくお願いいたします。
python
1from selenium import webdriver 2from selenium.webdriver.common.by import By 3import time 4 5chrome_driver_path = "/home/usrname/programming/developper_tools/chromedriver" 6 7driver = webdriver.Chrome(executable_path=chrome_driver_path) 8driver.get("http://orteil.dashnet.org/experiments/cookie/") 9cookie = driver.find_element(By.ID, "cookie") #クリックするクッキー 10 11 12store_tags= driver.find_elements(By.CSS_SELECTOR, "#store div") #アイテム 13 14store_id = [store.get_attribute("id") for store in store_tags] #アイテムのid(htmlのid属性) 15store_id = [store.replace(" ", "\ ") for store in store_id] #アイテムのid名のスペースの前バックスラッシュを入れる 16 17next_time = time.time() + 5 #クッキーをクリックし続ける5秒間 18end_time = time.time() + 5*60 #終了の時刻 19 20#動作の内容 21while True: 22 cookie.click() #クッキーをクリック 23 if next_time < time.time(): #5秒経過後にアイテムを買う 24 while True: #アイテムを何個も買う 25 all_items = {} #アイテムとidを格納する辞書 26 for store in store_id: #アイテムの値段とidの組み合わせを辞書に入れる 27 print(store) 28 price_tag = driver.find_element(By.CSS_SELECTOR, f"#{store} b").text # エラーの部分 29 price = price_tag.split() 30 try: 31 p = price[-1].replace(",","") 32 except IndexError: 33 pass 34 all_items[store] = int(p) 35 money_tag = driver.find_element( 36 By.CSS_SELECTOR, "#game #money").text.replace(",", "") #所持しているお金を求める 37 money = int(money_tag) 38 affordable_upgrade = {} #買うことができるアイテムとそのidの辞書 39 for id, cost in all_items.items(): 40 if money > cost: 41 affordable_upgrade[id] = cost 42 if len(affordable_upgrade) == 0: # 買うことができるアイテムがないならループを抜け出す 43 break 44 max_item_id = max(affordable_upgrade, key=affordable_upgrade.get) #買えるアイテムの中で一番高価なもの (いらないかも) 45 click_shop = driver.find_element(By.ID, max_item_id) #買うアイテムのid 46 click_shop.click() #アイテムを買う 47 next_time = time.time() + 5 #次の5秒間 48 if time.time() > end_time: #5分経過したら終わる 49 cookie_per_socond = driver.find_element(By.ID, "cps") 50 print(cookie_per_socond.text) 51 break
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/26 05:41
2022/09/26 14:56 編集