Python AttributeError(“move”u to需要WebElement(“移动”)AttributeError:move”u to需要在Selenium Webdriver中使用Action类时出现WebElement错误

Python AttributeError(“move”u to需要WebElement(“移动”)AttributeError:move”u to需要在Selenium Webdriver中使用Action类时出现WebElement错误,python,selenium,selenium-webdriver,webdriver,action,Python,Selenium,Selenium Webdriver,Webdriver,Action,链接到网站# 我想单击此图像中的红色按钮: 代码如下: act = ActionChains(driver) add_button = driver.find_elements_by_xpath("//button[@class='mp-Button mp-Button--secondary']") subDiv = driver.find_elements_by_xpath("//div[@class='phone-number-container']") sleep(5) actions

链接到网站#

我想单击此图像中的红色按钮:

代码如下:

act = ActionChains(driver)
add_button = driver.find_elements_by_xpath("//button[@class='mp-Button mp-Button--secondary']")
subDiv = driver.find_elements_by_xpath("//div[@class='phone-number-container']")
sleep(5)
actions = ActionChains(driver)
actions.move_to_element(add_button)
actions.click(subDiv)
actions.perform()
但我犯了这个错误:

  raise AttributeError("move_to requires a WebElement")
AttributeError: move_to requires a WebElement

请找到以下工作解决方案:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains

#utilise chrome driver to open specified webpage
driver = webdriver.Chrome(executable_path=r"\chromedriver.exe")
driver.maximize_window()
driver.get("https://www.2dehands.be/a/zakelijke-goederen/landbouw-veevoer/m1527886462-kleine-pakken-hooi.html?c=c41b759082ce0aa7411a74e54f8dbd13&previousPage=home")



actionChains = ActionChains(driver)
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, ".contact-options-mobile:nth-child(2) span:nth-child(2)")))
actionChains.move_to_element(element).click().perform()

element1 = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//span[@class='phone-number-bubble']")))
print element1.text

输出:0494383160

或者您可以通过xpath而不是xpath来查找元素