Pyautogui does not work as expected in Python 2.7

Asked 2 years ago, Updated 2 years ago, 125 views

I'm in trouble because the code doesn't work as expected.
I would appreciate it if you could let me know how to fix it.

environment

Python 2.7
Windows 7

Code Objectives

Paste pre-saved gazou1.png and gazou2.png on Gmail's email creation screen
以下 I saved it in filepath2 of the code below

Techniques

Open the image with paint and copy it with pyautogui → paste it → paste it into the body of Gmail → Ctrl+Z to restore the original image size.

symptoms

The first image often fails to paste.
Or rather, autobooting from the task scheduler almost fails (about 20% success rate).
Running the py file manually almost succeeds.

Operating on Failure

Open gazou1 with paint and Ctrl+A to make all selections successful
Subsequent Ctrl+C does not work on the paint screen.
When you focus on the text and paste , the most recently copied string is
Paste into the body of the email→Cancel the action with Crtl+Z

This phenomenon has occurred.
By the way, pasting the second image has always been successful.

Code

filepath2=u "C:\photo\"

app="C:\Windows\System32\mspaint.exe"
file=filepath2+u "gazou1.png"
pro=subprocess.Popen ([app, file])
time.sleep(3)

pyautogui.hotkey('ctrl', 'a')
pyautogui.hotkey('ctrl','c')

elements=driver.find_element_by_xpath(u"//*[@class='Am Aleditable LW-avf']")
elements.send_keys (Keys.ENTER)
ActionChains(driver).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
time.sleep(16)
ActionChains(driver).key_down(Keys.CONTROL).send_keys('z').key_up(Keys.CONTROL).perform()
time.sleep(4)
pro.kill()
time.sleep(3)

app="C:\Windows\System32\mspaint.exe"
file=filepath2+u "gazou2.png"
pro=subprocess.Popen ([app, file])
time.sleep(3)

pyautogui.hotkey('ctrl', 'a')
pyautogui.hotkey('ctrl','c')

elements=driver.find_element_by_xpath(u"//*[@class='Am Aleditable LW-avf']")
elements.send_keys (Keys.ENTER)
ActionChains(driver).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
time.sleep(16)
ActionChains(driver).key_down(Keys.CONTROL).send_keys('z').key_up(Keys.CONTROL).perform()
time.sleep(4)
pro.kill()
time.sleep(3)

python windows python2 pyautogui

2022-09-30 17:47

2 Answers

Based on the answers you gave me, I carried it out.
Unfortunately, similar symptoms have occurred and have not been successful.

Therefore, based on our way of thinking, I changed it as follows and it started to move steadily.

■Changed parts
filepath2=u"C:\\photo\\"
バックChange backslash to description twice in a row

Thank you to everyone who checked.


2022-09-30 17:47

I don't know if it's the root cause, but if you use the backslash \ in the string, it will be escaped, so I think the second " in the first line is escaped and the string is not closed.That's why StackOverflow's syntax highlights are weird.
When specifying a path in Python, you can usually use forward slash / even on Windows, so for now

filepath2=u"C:/photo/"
app="C:/Windows/System32/mspaint.exe"

Why don't you try it?

I mean, I think it's more mysterious to move 20% of the time...


2022-09-30 17:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.