python - AttributeError("move_to requires a WebElement") AttributeError: move_to requires a WebElement error using Action Class in Selenium Webdriver
问题描述
Link to website # https://www.2dehands.be/a/zakelijke-goederen/landbouw-veevoer/m1527886462-kleine-pakken-hooi.html?c=c41b759082ce0aa7411a74e54f8dbd13&previousPage=home
I want click this button red in this image:
.
Here is the code:
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()
But got this error:
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
推荐阅读
- excel - 如何在 VBA 中使用 sql 查询并将数据从一个 Excel 工作表获取到另一个工作表?
- python - Python 脚本和 Tweepy 的 Cron 作业
- excel - MURAL Web Scraping 编译错误 - 项目或库
- sql - sql查询返回事件链
- ruby-on-rails - 在 Rails 中使用一个模型和另一个模型上的两个属性创建连接表
- c# - 编译带有外部cs文件依赖的dll
- itfoxtec-identity-saml2 - 来自 AuthnRequest 的 AssertionConsumerServiceURL 不是来自 Config
- android - Android/iOS 应用商店要求一个简单的 Cordova 应用程序,然后将 Web 视图转发到外部 Javascript 应用程序
- java - 比较两个不同的数据类型对?
- c# - 如何在 AWS CDK PolicyStatement AddAction 方法中嵌套 KeyValue 对?