Python. Click on coordinates (WebScrapping) [6]

I thought I was done with Selenium,as it turned out no. Therefore, there will be a couple of articles on this library.

Preface.

In this tutorial, we will learn how to click on the coordinates of the window using Selenium. This feature will be useful when writing bots for online games.

Proceed.

To work with the mouse, we need the ActionChainsmodule.

ActionChains is a module that allows you to perform low-level operations such as:

  • mouse movement
  • mouse click (LKM)
  • work with context menu (PKM)

When you call the methods of the ActionChainsclass, the methods are queued. They are called by the perform()method, on a first-come, first-served basis. Read more in the documentation.

Draw a point on the screen.

Rewrite the following code:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver. Chrome('chromedriver.exe')
driver.set_window_size(500, 500)
driver.get('http://paintonline.editaraudio.com/')
ActionChains(driver).move_by_offset(250, 250).click().perform()

Save this code in the same folder as chromedriver.exe (Google Chome must be installed). Let's analyze the code.

from selenium.webdriver.common.action_chains import ActionChains

Import ActionChains

driver.set_window_size(500, 500)

Set the size of the browser window to 500×500 pixels.

driver.get('http://paintonline.editaraudio.com/')

Go to the website of the http://paintonline.editaraudio.com/

ActionChains(driver).move_by_offset(250, 250).click().perform()

Here we do a sequence of actions.

  • move_by_offset(250, 250) – move the cursor 250 pixels to the right and 250 pixels down.
  • click() – click.
  • perform() – perform all actions.

As a result, we drew a dot on the screen.

Draw 2 points on the screen.

Add the following lines to your code:

ActionChains(driver).reset_actions()
ActionChains(driver).move_by_offset(250, 300).click().perform()

Here, we reset the cursor position reset_actions(),now the position is 0.0. Then, click on the coordinates (250, 300) thereby drawing a point on the canvas.

Draw a line on the screen.

Delete the previous ActionChainscalls, and add the following one.

ActionChains(driver).move_by_offset(250, 250).click_and_hold().move_by_offset(20, 20).perform()

Let's analyze each action:

  • move_by_offset(250, 250) – move the cursor to coordinates 250, 250
  • click_and_hold()clamping the paintwork
  • move_by_offset(20, 20) – move the cursor 20 pixels to the right, and 20 down, to coordinates 270, 270.
  • perform() – perform all actions.

As a result, a diagonal line was obtained.

Conclusion

Using ActionChains,you can write a bot for almost any game.

Starting from clickers, like alien worlds.

Ending with farms, with the possibility of drag_and_drop.

Final result.

Пожалуйста отключи блокировщик рекламы, или внеси сайт в белый список!

Please disable your adblocker or whitelist this site!