How can I handle pyautogui screenshot error? - python

I'm trying to code "Keep what you are doing, when ss.png has seen on screen, do this instead" but it only prints None again and again. What should I do?
try:
image = gui.locateOnScreen("ss.png")
print(image)
except:
gui.click(296, 47, clicks=3, interval=0.25)
gui.press("enter")
time.sleep(2)
gui.click(499, 484, clicks=3, interval=0.25)
time.sleep(5)
gui.click(505, 585)
gui.press("enter")
time.sleep(5)
gui.click(133, 223)
time.sleep(2)
gui.click(477, 101)
time.sleep(3)
time.sleep(5)
gui.click(667, 376)
time.sleep(40)
gui.click(1225, 106)
time.sleep(2)
gui.click(1175, 269)
time.sleep(5)
counter = counter + 1

Related

auto-setting price for minted NFT collection on OpenSea

Im trying to sell minted NFT collection on OpenSea with this script:
# Set price
for i in range(10000): # See if it's there
try:
chrome.find_element(By.XPATH, '//input[#name="price"]')
break
except:
time.sleep(0.5)
chrome.find_element(By.XPATH, '//input[#name="price"]').send_keys(price + Keys.RETURN)
# Click sign
for i in range(10000):
try:
chrome.find_element(By.XPATH, '//button[text()="Sign"]')
break
except:
time.sleep(0.5)
chrome.find_element(By.XPATH, '//button[text()="Sign"]').click()
# scroll
chrome.find_element(By.XPATH, '//div[#data-testid="signature-request-scroll-button"]').click()
# waiting unblock button
time.sleep(1)
# sign
chrome.find_element(By.XPATH, '//button[text()="Sign"]').click()
and the script stuck when Metamask ask for sign transaction because of "scroll down" button

monitor chrome tabs - python selenium

I'm writing a script where I need you to monitor the number of open tabs. The script reads a table and each information in the table opens a new tab in the browser. It turns out that in order not to have thousands of tabs open, I need the script to monitor the tabs to have only 3 open, when I close a tab I need to automatically open the next tab
def base_dados_processos(self):
try:
df = pd.read_excel('TABLE.xlsx')
self.num_proc = df['COLUM']
except Exception:
pg.alert(text='error', title='ERROR', button='OK')
self.chrome.quit()
def loop_pesquisa(self):
for PROCESSOS in zip(self.num_proc):
num_current_tabs = len(self.chrome.window_handles)
if num_current_tabs < 3:
pg.hotkey('ctrl', '1')
time.sleep(1)
self.chrome.get('https://stackoverflow.com/')
pg.write(str(test))
pg.press('enter')
time.sleep(3)
pg.press('tab', presses=26)
pg.press('enter')
time.sleep(1)
pg.press('enter')
To do this, just add a loop that monitors the guides, it can be as follows:
while len(self.chrome.window_handles) > 3:
time.sleep(0.5)
without your code:
def base_dados_processos(self):
try:
df = pd.read_excel('TABLE.xlsx')
self.num_proc = df['COLUM']
except Exception:
pg.alert(text='error', title='ERROR', button='OK')
self.chrome.quit()
def loop_pesquisa(self):
for PROCESSOS in zip(self.num_proc):
while len(self.chrome.window_handles) > 3:
time.sleep(0.5)
pg.hotkey('ctrl', '1')
time.sleep(1)
self.chrome.get('https://stackoverflow.com/')
pg.write(str(test))
pg.press('enter')
time.sleep(3)
pg.press('tab', presses=26)
pg.press('enter')
time.sleep(1)
pg.press('enter')

I have a problem while executing the selenium script, it takes longer time to perform its Manuel testing

For example this statement takes 41 second to perform the Manuel testing:
try:
print("at2-click" + " - Time used=%ss" % int((time.time() - start_time)))
time.sleep(2)
wait_until_visible_then_click(element_by_id)
men_10 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="product-transaction-area"]/div[1]/div/div/div[2]/div/a[10]')))
men_10.click()
except exp.NoSuchElementException as e:
no_such_element_exption('at2', str(e))
time.sleep(3)
but this statement takes 160 seconds to perform its Manuel testing
try:
print("In den Wb2 product" + " - Time used=%ss" % int((time.time() - start_time)))
time.sleep(2)
element_by_id = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="product-transaction-area"]/div[3]/button')))
except exp.NoSuchElementException as e:
no_such_element_exption('In den Wb2', str(e))
try:
print("ZK" + " - Time used=%ss" % int((time.time() - start_time)))
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
element_by_class = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'button-big')))
except exp.NoSuchElementException as e:
no_such_element_exption('ZK', str(e))
I prefer to execute this 2nd statement between 50-99 seconds, Can anyone please help to solve this issues
The best solution for me would to avoid using webdriverwait.
You can do some pooling:
'''
driver.get('exammple.com')
found=False
while(not found):
elem = driver.find_element_by_class_name('example')
if elem:
found=True
found=False
while(not found):
elem = driver.find_element_by_class_name('example')
if elem:
found=True
'''

Need to have this repeat if a code is not received within x time

This portion of my code submits a request for sms verification. I want it to restart a portion of the script if the sms verification code is not received within 90 seconds, so it will restart that step and try for a new number.
EC.presence_of_element_located((By.XPATH, "//*[#id='mobile-container']/div/div/form/div[2]/div[4]/div/div/div/div[2]/button")))
time.sleep(0.5)
addPn.click()
time.sleep(1)
numberBox = driver.find_element(By.XPATH, "//*[#type='tel']")
global nId
global num
noNum = True
while noNum:
r = requests.get("https://sms-activate.ru/stubs/handler_api.php?api_key=00000000000000000&action=getNumber&service=ew&country=0")
if "ACCESS_NUMBER" in r.text:
info = r.text.split(":")
nId = str(info[1])
num = str(info[2])
noNum = False
c = driver.find_element(By.XPATH, "//*[#class='country']")
c.click()
z = driver.find_element(By.XPATH, "//*[#class='country']/option[38]")
z.click()
time.sleep(0.5)
numberBox.click()
for character in num[1:]:
numberBox.send_keys(character)
time.sleep(0.2)
sendCode = driver.find_element(By.XPATH, "//*[#class='sendCodeButton']")
sendCode.click()
requests.get(f"https://sms-activate.ru/stubs/handler_api.php?api_key=00000000000000000&action=setStatus&status=1&id={nId}")
code = ""
noCode = True
while noCode:
r = requests.get(f"https://sms-activate.ru/stubs/handler_api.php?api_key=00000000000000000&action=getStatus&id={nId}")
if "STATUS_OK" in r.text:
code = r.text.split(":")[1]
noCode = False
codeBox = driver.find_element(By.XPATH, "//*[#type='number']")
for character in code:
codeBox.send_keys(character)
time.sleep(0.5)
check = driver.find_element(By.XPATH, "//*[#class='checkbox']")
check.click()
time.sleep(0.4)
cont = driver.find_element(By.XPATH, "//*[#value='CONTINUE']")
cont.click()

Python selenium Element not clickable since another element obscures it

I have the following code. There is a problem in the line with:
timePicker = browser.find_element_by_xpath('//*[#class="time hasTimepicker"]')
It raises this exception:
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <input id="tp1594226550595" class="time hasTimepicker" type="text"> is not clickable at point (753,293) because another element <rect class="highcharts-background"> obscures it
I have already tried solutions which use 'wait', but there is a timeout exception. It seems the obstruction is permanent. I have also tried ActionChains, but it's not working either.
The element I am trying to click on is in this image:
The graph is the element obscuring the time picker at the top:
from time import sleep, time
from selenium import webdriver
from selenium.common.exceptions import \
NoSuchElementException, \
ElementClickInterceptedException, \
ElementNotInteractableException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", "C:/Users/USER/Documents/temp")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
print('opening browser')
browser = webdriver.Firefox(
executable_path='C:/Users/USER/Documents/Python Workspace/geckodriver/geckodriver.exe',
firefox_profile=fp
)
print('opening link')
browser.get('https://webtrader.binary.com/v2.2.8/main.html#historical-data')
def close_popup():
entrytime = time()
try:
currenttime = time()
if currenttime - entrytime >= 10:
return
close1 = browser.find_element_by_xpath('/html/body/div[3]/div[1]/div/button/span[1]')
except NoSuchElementException:
sleep(2)
close_popup()
try:
currenttime = time()
if currenttime - entrytime >= 10:
return
click_element(close1)
except UnboundLocalError:
currenttime = time()
if currenttime - entrytime >= 10:
return
sleep(2)
close_popup()
def click_element(element):
entrytime = time()
try:
currenttime = time()
if currenttime - entrytime >= 10:
return
element.click()
sleep(2)
except ElementClickInterceptedException:
currenttime = time()
if currenttime - entrytime >= 10:
return
sleep(2)
click_element(element)
except ElementNotInteractableException:
currenttime = time()
if currenttime - entrytime >= 10:
return
sleep(2)
click_element(element)
def resources():
try:
global res
res = browser.find_element_by_link_text('Resources')
res.click()
sleep(2)
except NoSuchElementException:
resources()
print('accessing Resources')
resources()
print()
#
#
def historical_data():
global res
try:
hd = browser.find_element_by_link_text('Historical Data')
sleep(2)
except ElementNotInteractableException:
sleep(2)
historical_data()
except NoSuchElementException:
sleep(2)
res.click()
historical_data()
try:
hd.click()
sleep(2)
except UnboundLocalError:
sleep(2)
historical_data()
# hd.click()
# sleep(2)
print('accessing Historical data')
historical_data()
maxWindow = browser.find_element_by_xpath('/html/body/div[8]/div[1]/div/a[3]/span')
browser.execute_script('arguments[0].click();', maxWindow)
sleep(2)
print('setting data type to 1 Tick')
while True:
try:
dt = browser.find_element_by_xpath('/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[1]/span/span[1]')
break
except NoSuchElementException:
sleep(2)
dt.click()
sleep(2)
tick = browser.find_element_by_xpath('/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[2]/div[1]/div[2]/span')
tick.click()
sleep(2)
#
while True:
try:
timePicker = browser.find_element_by_xpath('//*[#class="time hasTimepicker"]')
break
except NoSuchElementException:
sleep(2)
timePicker.click()
Try this instead:
while True:
try:
timePicker = browser.find_element_by_xpath('/html/body/div[8]/div[4]/input[2]')
break
except NoSuchElementException:
sleep(2)
timePicker.click()
Additionally, you import WebDriverWait, but don't use it. Try this, replace:
while True:
try:
dt = browser.find_element_by_xpath('/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[1]/span/span[1]')
break
except NoSuchElementException:
sleep(2)
dt.click()
sleep(2)
tick = browser.find_element_by_xpath('/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[2]/div[1]/div[2]/span')
tick.click()
sleep(2)
#
while True:
try:
timePicker = browser.find_element_by_xpath('//*[#class="time hasTimepicker"]')
break
except NoSuchElementException:
sleep(2)
timePicker.click()
with this:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[1]/span/span[1]'))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[2]/div[1]/div[2]/span'))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[8]/div[4]/input[2]'))).click()
You'll immediately notice its significantly faster, and cleaner. I'd replace all those while,try, and time.sleep() with WebDriverWait.

Categories

Resources