2015-01-09 34 views
2

我想做一個使用Selenium的Python程序來自動化UnovaRPG遊戲。它應該按順序執行以下操作:UnovaRPG automator:硒Python圖像錯誤

1)使用用戶名和密碼登錄。

2)去治療中心點擊治療。

3)(使用給定鏈路)撲滅教練員 「shedinja144」

4)選擇一個指定的小寵物。

5)在戰鬥中,它應該點擊最後一次攻擊後繼續。

6)然後點擊「回到地圖」爲最後一個寵物小精靈(第6)。

7)重複步驟2-5任意給定的次數。

這裏是我的代碼:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

driver = webdriver.Firefox() 
driver.get("https://www.unovarpg.com/login.php") 
assert "UnovaRPG" in driver.title 
i=1 
j=1 

typeUsername = driver.find_element_by_id("username") 
typePassword = driver.find_element_by_id("password") 

typeUsername.send_keys("******") 
typePassword.send_keys("******") 
driver.find_element_by_id("buttonLogin").click() 

while i<10: 
    driver.get("https://www.unovarpg.com/pokemon_center.php") 
    driver.find_element_by_id("healButton").click() 
    driver.get("https://www.unovarpg.com/battle.php?type=autotrainer&tid=5546527") 
    driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img").click() 
    while j<6: 
     driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[4]/div[6]/div[2]/ul/li[4]/a/strong").click() 
     driver.find_element_by_id("continue").click() 
     j+=1 
    driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[4]/div[6]/div[2]/ul/li[4]/a/strong").click() 
    driver.find_element_by_id("Back2MapButton").click() 
    i+=1 

以下是錯誤:

Traceback (most recent call last): 
    File "Selenium v1.py", line 21, in <module> 
    driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img").click() 
    File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 230, in find_element_by_xpath 
    return self.find_element(by=By.XPATH, value=xpath) 
    File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 662, in find_element 
    {'using': by, 'value': value})['value'] 
    File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 173, in execute 
    self.error_handler.check_response(response) 
    File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 166, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img"} 
Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/[email protected]/components/driver-component.js:9641:26) 
    at FirefoxDriver.prototype.findElement (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/[email protected]/components/driver-component.js:9650:3) 
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/[email protected]/components/command-processor.js:11635:16) 
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/[email protected]/components/command-processor.js:11640:7) 
    at DelayedCommand.prototype.execute/< (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/[email protected]/components/command-processor.js:11582:5) 

***Repl Closed*** 

錯誤在於無法找到寵物小精靈的形象點擊(步驟4)。我使用了從Firebug檢查器複製的xcode路徑。

幫助將不勝感激。

請注意,這僅用於創意目的,不會用於「作弊」。

+0

http://www.sikuli.org/可能是這樣的事情比Python更合適的平臺 –

回答

0

你需要explicitly wait for the element to load與它交互之前:

from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img")) 
)