首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnovaRPG自动程序: Selenium映像错误

UnovaRPG自动程序: Selenium映像错误
EN

Stack Overflow用户
提问于 2015-01-09 17:17:46
回答 1查看 265关注 0票数 2

我正在尝试使用Selenium制作一个python程序来自动化UnovaRPG游戏。它应该做以下工作:

1)使用用户名和密码登录。

2)转到康复中心,点击“治愈”。

3)与教练"shedinja144“战斗(使用给定的链接)

4)选择指定的口袋妖怪。

( 5)在战斗中,应单击最后一次攻击,然后继续攻击。

6)然后单击最后一个口袋妖怪(第6页)的“向后映射”。

7)重复任何给定次数的步骤2-5。

这是我的代码:

代码语言:javascript
复制
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

以下是错误:

代码语言:javascript
复制
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/fxdriver@googlecode.com/components/driver-component.js:9641:26)
    at FirefoxDriver.prototype.findElement (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver@googlecode.com/components/driver-component.js:9650:3)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver@googlecode.com/components/command-processor.js:11635:16)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver@googlecode.com/components/command-processor.js:11640:7)
    at DelayedCommand.prototype.execute/< (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver@googlecode.com/components/command-processor.js:11582:5)

***Repl Closed***

错误在于无法找到口袋妖怪的映像来单击(步骤4)。我使用了从Firebug检查器复制的xcode路径。

我会很感激你的帮助。

请注意,这只是为了创造性的目的,不会被用来“欺骗”.

EN

回答 1

Stack Overflow用户

发布于 2015-01-09 18:51:20

在与它交互之前,您需要使用显式地等待元素加载。

代码语言:javascript
复制
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"))
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27865808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档