-1

How can I go to an element and perform mouse hover action using only javascript and Python? for instance This is my code:

from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path = "chromedriver.exe")
driver.get(url)

btns = driver.find_elements_by_css_selector('il[class="reactions"]')

for btn in btns:
    **(I Want to Hover all 'btn' Element Using only "driver.execute_script" Method)**
    time.sleep(5)
    btn.click()
click()
Prophet
  • 32,350
  • 22
  • 54
  • 79
MD Kawsar
  • 313
  • 2
  • 12

1 Answers1

1

To perform mouse hover with Selenium you should use ActionChains library.
Also you should use Expected Conditions explicit wait to load all btns the elements.
With it your code will be as following:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(executable_path = "chromedriver.exe")
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get(url)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'il[class="reactions"]')))
time.sleep(1)
btns = driver.find_elements_by_css_selector('il[class="reactions"]')

for btn in btns:
    actions.move_to_element(btn).perform()
    time.sleep(0.5)
    btn.click()
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • This is Not Working for Me, for an infinity Loop on the same page the mouse hover keeps hovering on the past element, I needed a javascript executor for this. – MD Kawsar Feb 14 '22 at 07:47