0

I have a problem. I need to move the mouse to an element, and then raise the mouse 9 pixels up from this element.

classic = browser.find_element(By.CLASS_NAME,"surf-text")  
nadclassiknav = webdriver.ActionChains(browser).move_by_offset(0, -9)  
webdriver.ActionChains(browser).click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
PECTAPT
  • 21
  • 3

2 Answers2

2

To move the mouse to an element and then move the mouse 9 pixels up from this element you need to induce WebDriverWait for the visibility_of_element_located() to identify the element, move the mouse and finally perform() the action chains and you can use the following Locator Strategy:

ActionChains(browser).move_to_element(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "surf-text")))).move_by_offset(0, -9).perform()

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • For some reason, the console throws this error: Traceback (most recent call last) File "D:\PythonProjects\AvisoBot\bot.py", line 51, in ActionChains(browser).move_to_element(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "surf-text")))).move_by_offset(0, -9).perform() File "C:\Users\brusn\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until raise TimeoutException(message, screen, stacktrace) – PECTAPT Nov 18 '21 at 20:18
  • The error implies the element `(By.CLASS_NAME, "surf-text")` wasn't found. Does the line `classic = browser.find_element(By.CLASS_NAME,"surf-text")` locates the element? – undetected Selenium Nov 18 '21 at 20:19
  • on the site, the element from which you need to step back 9 pixels looks like this: Просмотр видеоролика – PECTAPT Nov 18 '21 at 20:22
  • We don't have any clue about either `Просмотр видеоролика` or `By.CLASS_NAME,"surf-text")`. I just have you the syntax to perform the task. Locating the element should be a separate question all together. – undetected Selenium Nov 18 '21 at 20:24
  • And how to display the cursor in selenium? Or at least to know his position? I need this so that I can configure the bot to work. – PECTAPT Nov 18 '21 at 20:36
  • Let me know if [this discussion](https://stackoverflow.com/questions/61932569/selenium-keyboard-action-that-will-enable-working-and-testing/61999536#61999536) answers your question in the above comment. – undetected Selenium Nov 18 '21 at 20:42
  • I didn't find anything in this discussion that could help me. I just need to output the cursor position. – PECTAPT Nov 18 '21 at 20:51
1

I managed to fix the problem by using window.scrollBy driver.execute_script("window.scrollBy(0,1000)","")

the decomention- https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollBy