Pyautogui For Loop Skips - python

i'm having an issue looping through a list of items and using the pyautogui.write function to input in items from each iterated items in a text box,basically what happens is that the loop skips sometimes two items or three items, here is my code
The name_list argument here is an actual list of names
def send_name(name_list):
time.sleep(4)
pyautogui.click(1024,227)
for name in name_list:
create_request = pyautogui.click(1195,239)
time.sleep(1)
acct = pyautogui.click(1018,392)
pyautogui.click(1053,427)
promo_code_box = pyautogui.click(1006,466)
pyautogui.write(name)
time.sleep(2)
pyautogui.doubleClick(880,436)
quantity = pyautogui.click(998,533)
submit = pyautogui.click(754,577)
time.sleep(1)
confirm = pyautogui.click(757,487)
pyautogui.click(778,245)
for instance if the name_list was ['prada','gucci','adidas','nike','puma','dior'] - the loop will skip like 2 items in here whereas i'll need it to loop through all the list
Thanks

Adding more sleep statements should ensure no skipping:
def send_name(name_list):
time.sleep(4)
pyautogui.click(1024,227)
for name in name_list:
time.sleep(1)
create_request = pyautogui.click(1195,239)
time.sleep(1)
acct = pyautogui.click(1018,392)
time.sleep(1)
pyautogui.click(1053,427)
time.sleep(1)
promo_code_box = pyautogui.click(1006,466)
time.sleep(1)
pyautogui.write(name)
time.sleep(2)
pyautogui.doubleClick(880,436)
time.sleep(1)
quantity = pyautogui.click(998,533)
time.sleep(1)
submit = pyautogui.click(754,577)
time.sleep(1)
confirm = pyautogui.click(757,487)
time.sleep(1)
pyautogui.click(778,245)

U can use
pyautogui.PAUSE = 1 `(or more)`
def send_name(name_list):
time.sleep(4)
pyautogui.click(1024,227)
for name in name_list:
create_request = pyautogui.click(1195,239)
time.sleep(1)
acct = pyautogui.click(1018,392)
pyautogui.click(1053,427)
promo_code_box = pyautogui.click(1006,466)
pyautogui.write(name)
time.sleep(2)
pyautogui.doubleClick(880,436)
quantity = pyautogui.click(998,533)
submit = pyautogui.click(754,577)
time.sleep(1)
confirm = pyautogui.click(757,487)
pyautogui.click(778,245)
for give seconds for the next step

Related

Sending an email consisting of a verification code and verify that e-mail with a countdown clock in streamlit

To check users' email addresses, I want to email a verification code to the user and confirm it within a minute. I tried with the script below, but the script stops after printing "t". It could be due to async function I used. But only with that library, I could run two functions simultaneously.
email_form = st.form(key='my_email_form',clear_on_submit=False)
email=email_form.text_input(label='Please enter your email address')
submit_e_button = email_form.form_submit_button(label='Send')
if submit_e_button:
with st.form(key='my_code'):
code = st.text_input(label='Enter code')
submit_button = st.form_submit_button(label='Confirm')
# global key
# global code
global t1
t1=time.time()
fixed_digits = 4
key=random.randrange(1111, 9999, fixed_digits)
sentmail2()
t=0
async def clocktime():
global t
ph = st.empty()
N = 1*60
for secs in range(N,0,-1):
mm, ss = secs//60, secs%60
ph.metric("Countdown", f"{mm:02d}:{ss:02d}")
time.sleep(1)
t2=time.time()
t=t2-t1
if ss==1:
ph.metric("Countdown", "Time is out")
async def submittion():
print(t)
if submit_button:
if float(t) > 1*60:
st.text('Enter before the time limit.')
b = False
# print("You have run out of time!")
if float(t) < 1*60:
print("code " + str(code))
print(key)
if submit_button:
if b == True:
print("code" + code)
print(key)
if str(code)==str(key):
st.text('succcess')
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
asyncio.ensure_future(clocktime())
asyncio.ensure_future(submittion())
loop.run_forever()

selenium click() method doesn't work after perfectly working a couple of hours before

I'm using selenium to like automatically some pictures. So the script searches for a word, clicks on the first picture, locates the like button then clicks on it.
The click button works to click on the picture but not to like it... I didn't change the identifier, so the class name is perfectly fine. I don't where the problem could come.
Here's my code :
'''
class Interact(Browser):
#click on the firt picture in the page
def firstpic_click(self):
time.sleep(2)
first = self.browser.find_element_by_class_name("kIKUG")
first.click()
#likes curent picture
def like_pic(self):
time.sleep(2)
like = self.browser.find_element_by_class_name("fr66n")
soup = bs(like.get_attribute('innerHTML'),'html.parser')
if (soup.find('svg')['aria-label'] == 'Like'):
like.click()
time.sleep(2)
#returns and clicks on next picture if any
def next_picture(self):
time.sleep(2)
try:
# nex = browser.find_element_by_xpath('//svg[#aria-label="Next"]')
next = self.browser.find_elements_by_class_name("wpO6b ")
soups = [bs(nextt.get_attribute('innerHTML'),'html.parser') for nextt in next]
for i in range(len(soups)):
#print(soups[i])
if (soups[i].find('svg')['aria-label'] == 'Next'):
next[i].click()
return next[i]
#nex = self.browser.find_element_by_xpath('//button[#class="wpO6b "]')
#time.sleep(1)
#return nex
except selenium.common.exceptions.NoSuchElementException:
return 0
#liking all the next pictures if any
def continue_liking(self):
while(True):
next_el = self.next_picture()
if next_el != False:
self.like_pic()
time.sleep(2)
next_el.click()
time.sleep(2)
else:
print("not found")
break
def word_search(self, search=1):
# word = input("what?")
word = "lol"
#search is the method of search
#looking for word in search box
if search == 0:
search_box = self.browser.find_element_by_xpath("//input[#aria-label='Search Input']")
search_box.send_keys("#"+word)
time.sleep(2)
#search_box.send_keys(Keys.RETURN)
#search_box.send_keys(Keys.RETURN)
search_box.submit()
time.sleep(5)
#type the website directly
if search == 1:
self.browser.get("https://www.instagram.com/explore/tags/" + word)
def liking_pictures(browser0):
browser0.implicitly_wait(5)
browser = Interact(browser0)
browser.word_search()
browser.firstpic_click()
browser.like_pic()
browser.next_picture()
browser.continue_liking()
time.sleep(10)
if __name__ == '__main__':
with browse() as browser0:
#unsubscribing(browser0)
liking_pictures(browser0)
'''
Thank you

How to reset input from keyboard after delay?

Simple "input" in python:
code = input("Entrer your code...")
processCode(code)
I need to ask the user for a password on a usb keyboard but without a screen (so the user doesn't see what he is typing). The text here is just for some tests. The sending is validated by the Enter key of course.
To make sure that the input is always blank when the user starts typing and sends his code, I will need to add a condition to this input.
I would need some sort of time counter which starts after each character entered and if the Enter key is not pressed for 10 seconds, the input will be automatically reset.
Here is an example of code that approximates your question. You can improve it or take inspiration from it:
import keyboard
import time
from threading import Thread
start_time = time.time()
saved_pwd = False
stop_thread = False
def dedupe(items):
seen = set()
for item in items:
if item not in seen:
yield item
seen.add(item)
def count_time():
global saved_pwd, start_time
while True:
lap_time = round(time.time() - start_time, 2)
global stop_thread
if lap_time >= 10:
print("Please Re-enter the password")
saved_pwd = True
break
elif stop_thread:
break
password = []
Thread(target=count_time).start()
while saved_pwd is False:
key = keyboard.read_key()
start_time = time.time()
if key == 'enter':
saved_pwd = True
stop_thread = True
else:
password.append(key)
print("Your pwd: ", ''.join(dedupe(password)))

Python - how to get updated link that was passed as an argument?

I'm passing a link as an argument in a thread, that I want to scrape the timestamp on. But in the function that the thread is pointing to, the timestamp value does not change, every time i'm rescraping it. How do you get timeLink to be dynamic and change every time it goes over the while loop? Here is the code:
def abcStart(timeLink):
while True:
res = timeLink
res.raise_for_status()
timestamp = BeautifulSoup(res.content, 'html.parser').find_all('b')
if timestamp[0].text == otherTimestamp[0].text:
work on something
break
if timestamp[0].text > otherTimestamp[0].text:
continue
else:
print('not yet')
time.sleep(30)
break
timelink = requests.get('http://example.com/somelink')
threadobj = threading.Thread(target=abcStart, args=(timelink))
threadobj.start()
threadobj.join()
It looks like there is only one http request being sent. On this line:
timelink = requests.get('http://example.com/somelink')
the abcStart() function is receiving the http response, and using that one value the whole time it is running. This will cause us to scrape the same page every time. If we want to have a different page to scrape for each loop iteration, we need to perform another http request each time. Something like this:
def abcStart(timeLink):
while True:
res = requests.get(timeLink) # send request here
res.raise_for_status()
timestamp = BeautifulSoup(res.content, 'html.parser').find_all('b')
if timestamp[0].text == otherTimestamp[0].text:
work on something
break
if timestamp[0].text > otherTimestamp[0].text:
continue
else:
print('not yet')
time.sleep(30)
break
timeLink = 'http://example.com/somelink' # declare url
threadobj = threading.Thread(target=abcStart, args=(timelink))
threadobj.start()
threadobj.join()
I guess you should move timeLink request inside your function:
def abcStart(timeLink):
while True:
res = requests.get('http://example.com/somelink')
res.raise_for_status()
timestamp = BeautifulSoup(res.content, 'html.parser').find_all('b')
if timestamp[0].text == otherTimestamp[0].text:
work on something
break
if timestamp[0].text > otherTimestamp[0].text:
continue
else:
print('not yet')
time.sleep(30)
break
threadobj = threading.Thread(target=abcStart, args=())
threadobj.start()
threadobj.join()

Calling a function within a function within a class

So there's this website that posts something I want to buy at a random time of day for a limited amount of time and I want to write something to send a message to my phone when a new url is posted to that webpage.
I planned on doing this by counting the number of links on the page (since it's rarely updated) and checking it every 5 minutes against what it was 5 minutes before that, then 5 minutes later check it against what it was 10 minutes before that, 5 minutes later check what it was 15 minutes before that... and if it's greater than what it originally was, send a message to my phone. Here's what I have so far:
class url_alert:
url = ''
def link_count(self):
notifyy=True
while notifyy:
try:
page = urllib.request.urlopen(self.url)
soup = bs(page, "lxml")
links=[]
for link in soup.findAll('a'):
links.append(link.get('href'))
notifyy=False
print('found', int(len(links)), 'links')
except:
print('Stop making so many requests')
time.sleep(60*5)
return len(links)
def phone(self):
self= phone
phone.message = client.messages.create(to="", from_="",body="")
print('notified')
def looper(self):
first_count = self.link_count()
print('outside while')
noty = True
while noty:
try:
second_count = self.link_count()
print('before compare')
if second_count == first_count:
self.phone()
noty = False
except:
print('not quite...')
time.sleep(60)
alert = url_alert()
alert.looper()
As a test, I decided to set the if statement that determines whether or not to send a message as equal but the loop kept on running. Am I calling the functions within the looper function the right way?
It looks like you need to eliminate the try block, as it is now, if self.phone() takes an exception you will never leave the loop
def looper(self):
first_count = self.link_count()
while True:
if first_count != self.link_count():
self.phone()
break
time.sleep(60)

Categories

Resources