2016-08-28 41 views
0

好吧,我是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) 

任何幫助或建議,將不勝感激,如果這不夠詳細,我很抱歉。

+0

硒是棘手的...就這麼說,它似乎是因爲在頁面上找不到元素而崩潰..您可能需要在點擊按鈕後添加一個time.sleep(3)以使頁面足夠重新加載元素的時間...同時確保頁面在提交完成後確實包含ID voucherCode。一個簡單的爲什麼要測試這是與鉻工具和手動進行檢查頁面中的更改html – reticentroot

+0

@reticentroot我已經嘗試了你說過的兩件事。 time.sleep()沒有幫助,同樣的錯誤再次。我也在提交後檢查了元素,它仍然存在,相同的ID和所有內容。 –

+0

緊隨你i + = 1之後,你強制selenium重新加載頁面(driver.get()),並在while循環中移動你的voucher_box變量和redeem_button變量。這樣每次都可以找到元素。最終,你可以將所有內容都包含在while循環中,除了塊之外,還可以包含異常通過或休眠。 – reticentroot

回答

1

這是我在評論

while i < amount: 
    try: 
     counter = 0 
     # make sure that the elements can be found 
     # try ten times and then break out of loop 
     while counter < 10: 
     try: 
      voucher_box = driver.find_element_by_id("voucherCode") 
      redeem_button = driver.find_element_by_id("redeemGiftCardButton") 
      break 
     except: 
      time.sleep(1) 
      counter += 1 

     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 
     driver.get("my page") # may not need this line because the elements were moved inside the while loop 
     time.sleep(3) # give the page enough time to load 
    except: 
     pass 

你也可以去,只要像做html.find(ID)中提到的想法> -1:做其他事情的ID WASN」 t出現在頁面上,因此退出或重新加載。此外,更那麼別的就可能更好地移動

voucher_box = driver.find_element_by_id("voucherCode") 
      redeem_button = driver.find_element_by_id("redeemGiftCardButton") 

while循環中,因爲,這樣的目標是通過everyloop正確的對象。可能發生的事情是,您正在使用屬於不同dom的元素。

+0

非常感謝,它的工作! –