0

I need some help to code in python with Appium to find element by image and click on the image.

If someone has a sample working code please share.

I tried following code:

import base64
from appium.webdriver.common.mobileby import MobileBy

with open(r"C:\Users\ADMIN\Desktop\flipkart.png", "rb") as image_file:
    image_base64 = base64.b64encode(image_file.read()).decode("utf-8")

caps = {
  "platformName": "Android",
  "appium:deviceName": "Galaxy M21",
  "appium:udid": "RZ8N50CSZWT",
  "noReset": True
}

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
driver.implicitly_wait(20)
element = driver.find_element_by_image(image_base64)
element.click()
driver.quit()

`

pmacfarlane
  • 3,057
  • 1
  • 7
  • 24

1 Answers1

0

I believe I have the solution to your problem, I recently had the same problem and managed to find an alternative by changing the element search function and adding an update_settings.

import base64
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
    
    
with open(r"C:\Users\ADMIN\Desktop\flipkart.png", "rb") as image_file:
    image_base64 = base64.b64encode(image_file.read()).decode("utf-8")

caps = {
      "platformName": "Android",
      "appium:deviceName": "Galaxy M21",
      "appium:udid": "RZ8N50CSZWT",
      "noReset": True
}
    
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)

driver.update_settings({"getMatchedImageResult": True})

driver.implicitly_wait(20)
element = driver.find_element(by=AppiumBy.IMAGE, value=image_base64)
element.click()
driver.quit()

And don't forget to turn on the appium server by enabling the images plugin

appium --base-path /wd/hub --use-plugins=images

If you don't have the images plugin installed, follow the command

appium plugin install images
ChrisMersi
  • 76
  • 9