【问题标题】:Calling ActionChains move_to_element() leads to attribute error?调用 ActionChains move_to_element() 导致属性错误?
【发布时间】:2016-06-01 18:22:32
【问题描述】:

运行 python/selenium 脚本时出现以下错误:

AttributeError:“WebElement”对象没有属性“move_to_element”

这个特定的错误信息说明了什么?是否没有将 move_to_element 识别为有效操作?我正在导入 actionchains 模块:

from selenium.webdriver.common.action_chains  import ActionChains

我可以毫无问题地使用 click() 和 send_keys() 操作。

def wait_for_element_visibility(self, waitTime, locatorMode, Locator):
     element = None
     if   locatorMode == LocatorMode.ID:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.visibility_of_element_located((By.ID, Locator)))
     elif locatorMode == LocatorMode.NAME:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.visibility_of_element_located((By.NAME, Locator)))
     elif locatorMode == LocatorMode.XPATH:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.visibility_of_element_located((By.XPATH, Locator)))
     elif locatorMode == LocatorMode.CSS_SELECTOR:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.visibility_of_element_located((By.CSS_SELECTOR, Locator)))
     else:
         raise Exception("Unsupported locator strategy.")
     return element

 def find_element(self, locatorMode, Locator):
     element = None
     if locatorMode == LocatorMode.ID:
        element = self.driver.find_element_by_id(Locator)
     elif locatorMode == LocatorMode.NAME:
        element = self.driver.find_element_by_name(Locator)
     elif locatorMode == LocatorMode.XPATH:
        element = self.driver.find_element_by_xpath(Locator)
     elif locatorMode == LocatorMode.CSS_SELECTOR: 
        element = self.driver.find_element_by_css_selector(Locator)
     else:
        raise Exception("Unsupported locator strategy.")
     return element

 def fill_out_field(self, locatorMode, Locator, text):
     self.find_element(locatorMode, Locator).clear()
     self.find_element(locatorMode, Locator).send_keys(text)

 def click(self, waitTime, locatorMode, Locator):
    self.wait_until_element_clickable(waitTime, locatorMode, Locator).click()

 def hover_over(self, waitTime, locatorMode, Locator):
     element = Locator
     self.wait_for_element_visibility(waitTime, locatorMode, Locator).move_to_element(element).perform()

我试着这样称呼它:

self.hover_over(10,
                        "id",
                        WelcomePageMap['ShareButtonId']
        ) 

【问题讨论】:

  • 显示完整代码。我不知道你为什么认为导入是我们需要的唯一信息。
  • 请分享您遇到错误的确切代码

标签: python selenium


【解决方案1】:

这是问题所在:

self.wait_for_element_visibility(waitTime, locatorMode, Locator).move_to_element(element).perform()

这里wait_for_element_visibility() 等待元素的可见性并返回元素本身,然后,您在WebElement 对象上调用move_to_element(),但是,您需要在ActionChains 实例上调用它,传递先前的找到元素作为参数:

from selenium.webdriver.common.action_chains import ActionChains

element = self.wait_for_element_visibility(waitTime, locatorMode, Locator)

actions = ActionChains(self.driver)
actions.move_to_element(element)
actions.perform()

【讨论】:

  • 好的,谢谢——我正在尝试将它包装在一个方法中,这样我就可以在另一个页面对象中调用它来测试脚本。那么 move_to_element() 不可能做到这一点?
  • @VinceL 当然,已更新以反映您发布的代码。希望对您有所帮助。
  • 非常感谢!现在这是有道理的。我不知道你不能像那样使用 move_to_element()。
  • @Vince 你错过了更多的东西。查看 selenium 的 readthedocs 页面,了解您可以做什么,以及您不需要自己编写什么。有一些方法可以等待元素,称为 expected_conditions,与 By() 结合使用,可以满足您的需要。无论如何,学会做自己不会伤害你……重点是阅读已经完成的事情。
【解决方案2】:

我认为您正在尝试将“move_to_element()”调用为 WebElement 对象扩展,但这是不可能的。即使导入 ActionChain,WebElement 也不会有这种扩展。

你需要做类似的事情:

ActionChains(driver).move_to_element(element).perform()

驱动程序 - 你的网络驱动程序

element - 您要移动到的元素(WebElement 对象)。

【讨论】:

    【解决方案3】:

    这是你的代码会失败的地方:

     self.wait_for_element_visibility(waitTime, locatorMode, Locator).move_to_element(element).perform()
    

    这里有一个在 python 中使用 Actions 的示例代码:

    element = self.driver.find_element_by_id(<id>) //use any of your locator strategy
    action = ActionChains(self.driver)
    action.move_to_element(element).perform()
    

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 1970-01-01
      • 2018-09-28
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 2021-06-01
      • 1970-01-01
      相关资源
      最近更新 更多