Creating unique characters with Python Selenium

I am interested in creating drawings of characters A B C D on the canvas at using Selenium Action chains.

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

def draw(action, offset_list):
    for offset in offset_list:
        action.click_and_hold().move_by_offset(offset[0], offset[1]).perform()
        print(f'Moving to {offset[0]}, {offset[1]}')
        time.sleep(1)
        action.release().perform()

def main(driver):
        driver.get("https://sketchtoy.com/")
        action = ActionChains(driver)
        element = driver.find_element_by_xpath("html/body/div[1]/div[5]/div[2]/canvas")
        action.move_to_element(element).perform()
        draw(action, [(-100, -100), (100, -100), (-100, 100), (100, 100), (-100, -100)])

This code should create a square shape by moving between coordinates similar to an X-Y graph axis.

However, instead of a square, it appears to be drawing a triangle.

https://i.stack.imgur.com/pYEJK.png

I'm unsure why it's behaving this way and how to correct it.

Answer №1

This container contains a box, allowing you to move it using offset values. For example, using move_by_offset(50,0) will shift the box to the right by 50 units, while move_by_offset(0,50) moves it down by 50 units. You have the option to either round the edges of the box by moving it with an offset or releasing it altogether.

def main(driver):
        driver.get("https://sketchtoy.com/")
        action = ActionChains(driver)
        element = driver.find_element_by_xpath("html/body/div[1]/div[5]/div[2]/canvas")
        action.click_and_hold(element).move_by_offset(50,0).move_by_offset(5,0)
        action.move_by_offset(0,50)
        action.move_by_offset(-5,0).move_by_offset(-50,0).move_by_offset(-5,0)
        action.move_by_offset(0,-100)
        action.release()
        action.perform()

Answer №2

My Experience Moving to Coordinates and Drawing with Actions

def sketch(action, element, offset_list):
    action.click_and_hold(element)
    for offset in offset_list:
        action.move_by_offset(offset[0], offset[1])
        time.sleep(1)
    action.release().perform()

def initiate(driver):
    driver.get("https://sketchtoy.com/")
    action = ActionChains(driver)
    element = driver.find_element_by_xpath("html/body/div[1]/div[5]/div[2]/canvas")
    action.move_to_element(element).perform()
    sketch(action, element, [(0,20), (0,5), (20, 0), (0,-5), (0,-20), (0, -5), (-20, 0), (0, 10)])
    sketch(action, element, [(70, 0), (5, 0), (0, 25), (-20, 0), (0, -5)])
    sketch(action, element, [(0, 60), (0, 10), (75, 0), (0, -10), (-5, 0)])

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Grabbing Checkbox Labels Using Selenium WebDriver

Hello, I am currently attempting to retrieve the name located next to the checkbox within the same span class. The name is contained within the label tag. Unfortunately, when using the getText() method, I am receiving an empty name. Below is how I am atte ...

Python script created in Abaqus to convert temperature measurements in a specific .txt file

Currently, I am working with a python script that is able to extract temperature data from my odb file. However, the output of the temperature is in degrees Fahrenheit but I need it in degrees Celsius for our customers. Is there a way to modify the scrip ...

What is the most effective method for setting up scheduled script tasks (primarily in Python) on a Mac that is comparable to the functionality

While Windows Scheduler may not have been the most attractive tool, it did offer a user-friendly GUI and plenty of customization options (such as failure actions, retry timing, and scheduling). With my Mac, I'm looking for something similar to run Pyt ...

Learn how to detect a continuous mouse click using Python

When working with Turtle in Python, I have discovered the onscreenclick function that allows me to read a mouse click and execute the specified action. However, I am now interested in making the function continuously run while I hold down the mouse click. ...

How do I transition from Selenium RC to WebDriver in PHP?

After creating a Selenium test case with PHP, I am looking to transition it to WebDriver. How should I proceed? ...

The order in which the test cases are executed by TestNG has been altered with the latest update to version 6.14

After updating the TestNG version to 6.14.2, I started encountering issues with running sequences that were not present when using version 6.8.8. Despite trying various solutions such as changing priorities, the tests did not run as expected. For more deta ...

Guide on using Selenium and Python with the buster extension to easily navigate through ReCaptcha

I am currently automating certain processes using Selenium and encountering the challenge of solving Google ReCaptcha. The technology I use to tackle this issue is the browser Plugin Buster. To enter the Google ReCaptcha, I employ the following code: drive ...

Discover sections of text using OpenCV / identify document structure

I have a color image document containing text, images, and tables. The document may be formatted with two columns and is composed of different sections: a header area, text section (with larger font and possible variations in font color), and sub-header wi ...

Encountering a critical issue while trying to convert a game created in pygame to

I have written the following code to generate the build file: import cx_Freeze executables = [cx_Freeze.Executable("SpeedRacer.py")] cx_Freeze.setup( name="SpeedRacer", options={"build_exe": {"packages": ["pygame"],"include_files": ["SpeedRacerD ...

python The program will not be terminated abruptly when calling selenium driver.quit()

The code snippet above is intended to terminate the program when an exception occurs by calling driver.quit(). However, despite this, the program continues to run. What did I miss here? try: driver.refresh() wait.until(ec.visibility_of_element_lo ...

Comparison of differences between Django and Google App Engine

I am currently working on a Django wiki project and I intend to deploy it on Google App Engine. Does anyone know if it is feasible to implement a text difference system, similar to textdiff, within App Engine? ...

In the latest version of Qiskit 0.24.0 tutorial on Max-Cut and Traveling Salesman Problem, learn the method to tackle the TSP problem involving more than three nodes

I recently experimented with qiskit's Traveling Salesman Problem example, starting with 3 nodes and running it on IBM's simulator_statevector backend. The execution went smoothly and the results were as expected. However, I decided to challenge ...

Testing simultaneously two pages with Selenium

Currently, I am working with two web pages A and B. My goal is to ensure that any changes made on page A are correctly reflected on page B. To achieve this, I plan to run two simultaneous Selenium tests - one for page B and another for page A. The test for ...

What is the most effective approach for managing a nested for loop?

I am facing an issue while attempting to save records into my database based on the selected options in a form. The code I have been working with keeps throwing an error stating that 'j' does not exist in the context of previouscourselist. if r ...

The parent class initializes web elements for the child class by using PageFactory.initElements

Why are child class elements being initialized when PageFactory.initElements is used in the parent class? How does this work? public class PageBase { public PageBase(WebDriver dr) { this.dr=dr; PageFactory.initElements(dr, this); ...

Python and Selenium are struggling to locate an element that should be clearly visible

I'm currently working on a project in Python using Selenium to extract data from the following website: However, I've encountered an issue where after entering the username and password, the "Submit" button does not seem to work. I would greatly ...

I am unable to execute this Python script

Having an issue with a syntax error while running my Python program. age = int(input('how old are you?22') Next_year_age = age + 1 print(f'on my next birthday, I will be {next_year_age}.') ...

Visualizing text data extracted from a CSV stream using Python

I am facing a challenge due to my limited experience in Python programming. I have been struggling with plotting the data and values obtained from a csv stream. Here is a snippet of code that I currently have, which allows me to print the real-time data s ...

Navigating the ins and outs of Appium installation and usage

Is it possible to use Appium without the need to install it on your machine? For example, is there a way to utilize the Appium library without actually installing it? If so, what steps would be involved in this process? ...

Troubleshooting: Dealing with the NoSuchElementException error in Internet Explorer with

I'm currently in the process of transitioning a Selenium script originally created for an internal Webapp from Selenium IDE to a Selenium Grid setup with RemoteWebDrivers in Java. While the scripts function properly when using Chrome and Firefox, the ...