AttributeError("move_to requires a WebElement") AttributeError: move_to requires a WebElement error using Action Class in Selenium Webdriver
AttributeError("move_to requires a WebElement") AttributeError: move_to requires a WebElement error using Action Class in Selenium Webdriver
我想单击此图像中的红色按钮:
.
代码如下:
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
或者您可以直接给出 find_element_by_xpath 而不是 find_elements_by_xpath
我想单击此图像中的红色按钮:
代码如下:
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
或者您可以直接给出 find_element_by_xpath 而不是 find_elements_by_xpath