Why does locateOnScreen return None?

Viewed 18

This is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By
import pyautogui as pt

#Variables to be Input
username = "standard_user"
password = "secret_sauce"
url = "https://www.saucedemo.com/"

#Opens Browser
driver = webdriver.Chrome("/usr/bin/chromedriver")

#Search for Website
driver.get(url)

#Find elements by name and input / click (into) them
driver.find_element(By.NAME, "user-name").send_keys(username)
driver.find_element(By.NAME, "password").send_keys(password)
driver.find_element(By.CSS_SELECTOR, "input[type=\"submit\" i]").click()

pos1 = pt.locateOnScreen('test.png', confidence=.6)
x = pos1[0]
y = pos1[1]
pt.moveTo(x, y)

I'm trying to make my code find the object in the test.png picture and it only returns none. I checked and test.png is in the correct folder. pt.locateOnScreen('test.png', confidence=.6) returns none at the moment. The test.png has the correct content.

1 Answers
#Modules needed for Automation Software
from selenium import webdriver
from selenium.webdriver.common.by import By
import pyautogui as pt

#Variables to be Input
username = "standard_user"
password = "secret_sauce"
url = "https://www.saucedemo.com/"

#Opens Browser
driver = webdriver.Chrome("/usr/bin/chromedriver")

#Search for Website
driver.get(url)

#Find elements by name and input / click (into) them
driver.find_element(By.NAME, "user-name").send_keys(username)
driver.find_element(By.NAME, "password").send_keys(password)
driver.find_element(By.CSS_SELECTOR, "input[type=\"submit\" i]").click()
#driver.find_element(By.CSS_SELECTOR, ".product_sort_container").click()

pt.moveTo(70, 100)
pos1 = pt.locateOnScreen('test.png', confidence=.6)
x = pos1[0]
y = pos1[1]
pt.moveTo(x, y)

I had to move the mouse onto the browser window using: pt.moveTo(70, 100)

Related