Find function isn´t find images

Find is not finding the images on the screen. I’ve already tested with several different images and none of them work

desktop_bot.type_keys([“win”, “d”])

my find:
if not self.find(“pasta”, matching=0.97, waiting_time=10000):
self.not_found(“pasta”)
self.move()

print:

Response:
Element not found: pasta
Traceback (most recent call last):
File “/mnt/linux/trabalho/scrapy-tim/buscar_nome_telefone_por_cep/buscar_nome_telefone_por_cep/bot.py”, line 41, in
Bot.main()
File “/mnt/linux/trabalho/scrapy-tim/.venv/lib/python3.10/site-packages/botcity/base/bot.py”, line 142, in main
bot.action(execution)
File “/mnt/linux/trabalho/scrapy-tim/buscar_nome_telefone_por_cep/buscar_nome_telefone_por_cep/bot.py”, line 27, in action
self.move()
File “/mnt/linux/trabalho/scrapy-tim/.venv/lib/python3.10/site-packages/botcity/base/utils.py”, line 45, in wrapper
raise ValueError(f’Element not available. Cannot invoke {func.name}.')
ValueError: Element not available. Cannot invoke move.

Hi @leandro23!

What is the structure of your project?

In the code, you are using “desktop_bot” to press the keys, but you are using “self” when doing the find.

Is the “self” also referencing a DesktopBot instance? Could you send me the complete code, please?

Hi @joaovoltarelli ! Thanks for listening.

The project structure was generated by cookiecutter for desktop and web applications (option 3)

You've downloaded /home/leandro/.cookiecutters/bot-python-template before. Is it okay to delete and re-download it? [yes]: yes
Select project_type:
1 - Desktop
2 - Web
3 - Both
4 - Custom

This is the content of bot.py file

from botcity.core import DesktopBot
from botcity.web import WebBot, Browser
from dotenv import load_dotenv

# from os import environ

load_dotenv()


class Bot(WebBot):
    def action(self, execution=None):
        self.headless = False
        desktop_bot = DesktopBot()
        # self.browser = Browser.CHROME

        # self.driver_path = "<path to your WebDriver binary>"
        # caminho_driver = environ["CAMINHO_CHROMEDRIVER"]
        # self.driver_path = caminho_driver
        # self.browse("https://www.google.com/")
        desktop_bot.type_keys(["win", "d"])

        if not self.find(
            "pasta",
            matching=0.97,
            waiting_time=5000,
        ):
            self.not_found("pasta")
        self.move()
        # Stop the browser and clean up
        # self.wait(5000)
        # self.stop_browser()

    def not_found(self, label):
        print(f"Element not found: {label}")


if __name__ == "__main__":
    Bot.main()

Using desktop_bot to find the image i got the same error.

From here I don’t know if it’s related to my problem.

I tried to create a bot version using pyautogui and I got a similar error. I solved this problem in the pyautogui application by setting confidence to 0.7 and installing opencv-python.

img = pyautogui.locateCenterOnScreen(
    path, confidence=0.7, grayscale=False
)

I tried to install opencv-python in the botcity application and set matching=0.7 but got the same error

        if not self.find(
            "pasta",
            matching=0.7,
            waiting_time=5000,
        ):
            self.not_found("pasta")
        self.move()

@leandro23 I think I found the problem,

class Bot(WebBot) is referencing a WebBot, so when you use “self”, you are referencing a WebBot, but the WebBot only has access to the browser context, so it does not find the element that is outside the browser context.

In this case, you can switch from “self” to “desktop_bot”, when performing operations with the DesktopBot instance

...

desktop_bot.type_keys(["win", "d"])

if not desktop_bot.find("pasta", matching=0.97, waiting_time=5000):
    self.not_found("pasta")
desktop_bot.move()

...

Basically, just remember to use “desktop_bot” for desktop operations and use “self” when you want to perform web browser operations.