运行我的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']
)
答案 0 :(得分:2)
以下是问题专栏:
self.wait_for_element_visibility(waitTime, locatorMode, Locator).move_to_element(element).perform()
此处wait_for_element_visibility()
等待元素的可见性并返回元素本身,然后,您在move_to_element()
对象上调用WebElement
,但是,您需要在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()
上调用它{3}}实例将先前找到的元素作为参数传递:
wait_timeout
答案 1 :(得分:0)
这是您的代码失败的地方:
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()
答案 2 :(得分:0)
我认为你试图将'move_to_element()'称为WebElement对象扩展,但这是不可能的。即使您导入ActionChain,WebElement也不会有这种扩展名。
你需要做类似的事情:
ActionChains(driver).move_to_element(element).perform()
驱动程序 - 您的WebDriver
元素 - 要移动到的元素(WebElement对象)。