运行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()
等待元素的可见性并返回元素本身,然后,您在move_to_element()
对象上调用WebElement
,但是,您需要在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/