AttributeError («move_to требует WebElement») AttributeError: move_to требует ошибки WebElement с использованием класса действий в Selenium Webdriver - PullRequest
0 голосов
/ 23 марта 2020

Ссылка на сайт # https://www.2dehands.be/a/zakelijke-goederen/landbouw-veevoer/m1527886462-kleine-pakken-hooi.html?c=c41b759082ce0aa7411a74e54f8dbd13&previousPage=home

Я хочу нажать на эту кнопку красным на этом изображении:

enter image description here.

Здесь код:

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

1 Ответ

0 голосов
/ 23 марта 2020

Ниже приведен рабочий раствор:

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

...