好吧,我是16歲,並且是Python新手。我需要做的項目,以幫助我學習,所以我想出了一個PlayStation加代碼生成器作爲一個項目,到目前爲止,它的作用:嘗試使用Selenium Python自動化優惠券代碼輸入
-Generates代碼 -logs在索尼的網站 -Enters到賬戶管理在代碼段贖回 - 檢查是否發生錯誤
voucher_box = driver.find_element_by_id("voucherCode")
redeem_button = driver.find_element_by_id("redeemGiftCardButton")
while i < amount:
voucher_box.clear()
currentcode = codegen.codegen()
voucher_box.send_keys(currentcode)
redeem_button.click()
if "The Prepaid Card code you entered is incorrect or is no longer valid" in driver.page_source:
print("Error found")
else:
print("Error not found")
i += 1
它的工作原理完全正常,如果只進行一次,但如果舉例來說,我設置金額2,我讓我的錯誤消息「發現」,然後崩潰和給我這個:
Traceback (most recent call last):
File "C:/Users/M4SS3CR3/PycharmProjects/UKP/main.py", line 38, in <module>
voucher_box.clear()
File "C:\Users\M4SS3CR3\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 87, in clear
self._execute(Command.CLEAR_ELEMENT)
File "C:\Users\M4SS3CR3\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 461, in _execute
return self._parent.execute(command, params)
File "C:\Users\M4SS3CR3\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Users\M4SS3CR3\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9454)
at Utils.getElementAt (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/[email protected]/components/command-processor.js:9039)
at fxdriver.preconditions.visible (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/[email protected]/components/command-processor.js:10090)
at DelayedCommand.prototype.checkPreconditions_ (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/[email protected]/components/command-processor.js:12644)
at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/[email protected]/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/[email protected]/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///C:/Users/M4SS3CR3/AppData/Local/Temp/tmpn3rxlnty/extensions/[email protected]/components/command-processor.js:12608)
任何幫助或建議,將不勝感激,如果這不夠詳細,我很抱歉。
硒是棘手的...就這麼說,它似乎是因爲在頁面上找不到元素而崩潰..您可能需要在點擊按鈕後添加一個time.sleep(3)以使頁面足夠重新加載元素的時間...同時確保頁面在提交完成後確實包含ID voucherCode。一個簡單的爲什麼要測試這是與鉻工具和手動進行檢查頁面中的更改html – reticentroot
@reticentroot我已經嘗試了你說過的兩件事。 time.sleep()沒有幫助,同樣的錯誤再次。我也在提交後檢查了元素,它仍然存在,相同的ID和所有內容。 –
緊隨你i + = 1之後,你強制selenium重新加載頁面(driver.get()),並在while循環中移動你的voucher_box變量和redeem_button變量。這樣每次都可以找到元素。最終,你可以將所有內容都包含在while循環中,除了塊之外,還可以包含異常通過或休眠。 – reticentroot