user4417794
user4417794

Reputation:

python selenium mouse scroll wheel click

I have a question about is it possible to simulate a mouse scroll wheel click in python selenium ( when you click on a link a new tab opens in the browser ) or something similar. The website I am using is javascript based so I can`t really see physical links.

Upvotes: 4

Views: 5181

Answers (2)

Kairat Koibagarov
Kairat Koibagarov

Reputation: 1475

You need to execute some javascript code.

 browser.execute_script("window.scrollBy(0,500)")
 time.sleep(3)
 browser.execute_script("window.scrollBy(0,500)")
 time.sleep(3)

This command to scroll the mouse down two times.

Upvotes: -1

Piotr Dawidiuk
Piotr Dawidiuk

Reputation: 3092

You need to execute javascript code. Mouse scroll wheel click has 1 as number representation according to MouseEvent.button documentation:

0: Main button pressed, usually the left button or the un-initialized state

1: Auxiliary button pressed, usually the wheel button or the middle button (if present)

2: Secondary button pressed, usually the right button

3: Fourth button, typically the Browser Back button

4: Fifth button, typically the Browser Forward button

Your javascript code will be

var mouseWheelClick = new MouseEvent( "click", { "button": 1, "which": 1 });
document.getElementById('#elementToClick').dispatchEvent(mouseWheelClick)

Then just simply

driver = webdriver.Firefox()
driver.execute_script(javascript_code)

Upvotes: 5

Related Questions