gpt4 book ai didi

python - 调用 ActionChains move_to_element() 会导致属性错误?

转载 作者:行者123 更新时间:2023-12-01 03:55:20 59 4
gpt4 key购买 nike

运行 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']
)

最佳答案

这是问题行:

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()

关于python - 调用 ActionChains move_to_element() 会导致属性错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37576123/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com