How do i paste my code in the python terminal using pyautogui? - python

coin = input("coin: ")
pyautogui.click(button='right')
pyautogui.press('enter')
print(coin)
after the input came out, pyautogui cant run the next line which is right click to paste the code in terminal. Is there any other solution to paste my code and save it in a var?

First of all, you would be better of using the shortcut "ctrl + v" to paste it, like so:
pyautogui.hotkey('ctrl', 'v')
However if your end goal is too get what is in your clipboard into a variable you could use this:
import win32clipboard
def getclipdata():
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
return data
To use the function do this whenever you want the clipboard data in a variable:
clipdata = getclipdata()
I hope this helped, feel free to ask me any questions.

Related

Copy the link, once click on a button - selenium python

There is a button on the page that when I clicked copies a link. check the below image
I'm trying to store that link in a variable with python selenium this way:
surl=ii.find_element(By.XPATH, f'/html/body/div[1]/div[2]/div[5]/div[1]/div/div/div/section/div/div[2]/div/div[2]/div[1]/div/div/div/div[3]/div[2]/table/tbody/tr[str({row+1})]/td[6]/div/div/span/div/button')
furl = surl.send_keys(Keys.CONTROL + "c")
print(furl)
But I get the result as None. What is the issue here, Could someone help me with this?
Thank you.
By clicking that button the link is copied to the clipboard.
There are several ways to get the text content from clipboard with Python. For example try this:
from tkinter import Tk
surl=ii.find_element(By.XPATH, f'/html/body/div[1]/div[2]/div[5]/div[1]/div/div/div/section/div/div[2]/div/div[2]/div[1]/div/div/div/div[3]/div[2]/table/tbody/tr[str({row+1})]/td[6]/div/div/span/div/button')
surl.click()
link = Tk().clipboard_get()
print(link)

python 3 - functions exercise download an image (import urllib.request + random)

Python 3, functions.
There is the following exercise:
In this exercise, we will write code that allows us to
download an image from the web! We will use an external module and understand how to read the documentation of the functions and use them.
We want to write a function that accepts a photo Url and
downloads the photo to your computer.
You can choose any picture you want from the Internet by clicking the right button and selecting "Copy Image Url”.
the image I chose:
https://images.theconversation.com/files/377569/original/file-20210107-17-q20ja9.jpg?ixlib=rb-1.1.0&rect=278%2C340%2C4644%2C3098&q=45&auto=format&w=926&fit=clip
the wanted name:
"4.jpg"
the given instructions:
Write code that selects a random number between 1 and 1000. The number will be the file name (search how to select a random number in python).
The image name has to have the right suffix, so concat“.png" to the end of the number you selected (You can also use .jpg, .bmp, etc).
For example: if the selected number was 2T0, you should have a variable that holds the string: “270.png”.
Import module urllib.request: https://docs.python.org/3/library/urllib.request.html#module-urllib.request
Read "how to use the urlretrieve function":
http://shecodesconnect.com/shecodes_python_blog/urlretrieve_en.php?lang=%27en%27
Use the function you read about in order to
download the image and save it under the name
you prepared.
Run the code you wrote and check the folder where your code is saved if the image was downloaded!
my code that doesn't work:
import urllib.request
local_filename, headers = urllib.request.urlretrieve(https://images.theconversation.com/files/377569/original/file-20210107-17-q20ja9.jpg?ixlib=rb-1.1.0&rect=278%2C340%2C4644%2C3098&q=45&auto=format&w=926&fit=clip)
html = open(local_filename)
html.close()
headers.items()
headers["content-type"]
image_url="https://images.theconversation.com/files/377569/original/file-20210107-17-q20ja9.jpg?ixlib=rb-1.1.0&rect=278%2C340%2C4644%2C3098&q=45&auto=format&w=926&fit=clip.jpg"urllib.request.urlretrieve(url=image_url, filename="4.jpg")
import random()
a=random.randint(1,1000)
print(4)
Hope you could help, my code doesn't work, thank you in advance!
After fixing the errors, looks like you end up with the below:
import urllib.request
local_filename, headers = urllib.request.urlretrieve('https://images.theconversation.com/files/377569/original/file-20210107-17-q20ja9.jpg?ixlib=rb-1.1.0&rect=278%2C340%2C4644%2C3098&q=45&auto=format&w=926&fit=clip')
html = open(local_filename)
html.close()
headers.items()
headers["content-type"]
image_url="https://images.theconversation.com/files/377569/original/file-20210107-17-q20ja9.jpg?ixlib=rb-1.1.0&rect=278%2C340%2C4644%2C3098&q=45&auto=format&w=926&fit=clip.jpg"
urllib.request.urlretrieve(url=image_url, filename="4.jpg")
import random
a=random.randint(1,1000)
print(4)
Assuming the code in the question is what were you were able to come up with, that is.

How do I press alt <numpad> with pyautoguii?

Here's my script:
import pyautogui
import subprocess
import time
#notepad is in the file path
programma = "Notepad"
#opening notepad
subprocess.Popen(programma)
#gives time to open notepad, ie I've also tried with higher numbers
time.sleep(1)
pyautogui.keyDown('NumLock')
#I've also tried with
pyautogui.hotkey('alt', 'num3')
I've tried and the Numpad works: if I comment the take out 'alt' and just write
pyautogui.press('num3')
it types the number 3.
Although if I put alt in the code, it gives an error, as if I were typing without having the cursor set.
I've also tried
pyautogui.keyDown('alt')
pyautogui.keyDown('num3')
pyautogui.keyUp('num3')
pyautogui.keyUp('alt')
Does anyone know what to do?
Thanks in advance
Working for me:
pyautogui.keyDown('alt')
pyautogui.press("num3")
pyautogui.keyUp('alt')
If you try to .keyDown('num3') pyautogui will think you want to use a third key in your combination.

Application().connect in PyWinAuto Cannot Find the Element I want

I am using Selenium& Chrome Webdriver and I need to upload a Image file.
Since I cannot send the file using SendKeys, I am tring to handle the windows File Browser using PyWinAuto.
So after I click the find file button using selenium, I have to use PyWinAuto to find the windows file browser that has been opened, So I have used Applications().connect.
This is the code I need help with.
app=Application().connect(title_re="Open")
app.FileUpload.Edit.SetText("screenshot.png")
time.sleep(5)
app.FileUpload.Button.click()
The Error comes on the first row of the code, Which says
ElementNotFoundError: {'title_re': 'Open', 'backend': 'win32', 'visible_only': False}
I do not understand why the Element Cannot be found.
I upload a picture of the windows File browser I need to find.
The error that you are getting, looks like it's because it cannot find an application with the title of "Open". In your screenshot, it still looks like that's a Chrome window rather than a Window Explorer window.
The best way to know, is to use a tool to find different elements, a great one is 'inspect.exe' that comes standard on windows. Instructions on how to find this are here.
I've also found that sometimes, it's needed to use Desktop() rather than Application().
I suggest using uiautomation:
import uiautomation
path = uiautomation.EditControl(Name = "File name:")
path.SetFocus()
path.SendKeys(file_path)
btn = uiautomation.ButtonControl(Name = "Open")
btn.SetFocus()
btn.Invoke()
fil = uiautomation.EditControl(Name="File name:")
fil.SetFocus()
fil.SendKeys(file_name + '{Enter}')
You can use SendKeys(file_path + file_name + '{Enter}'), but in python the '\' is not dectected as a string, so I have to split them into 2 part.
With uiautomation, it works good for me, Here is the code
path = uiautomation.EditControl(Name = "File name:")
path.SetFocus()
path.SendKeys(file) #file=filepath
btn = uiautomation.ButtonControl(Name = "Open")
btn.SetFocus()
fil = uiautomation.EditControl(Name="File name:")
fil.SetFocus()
fil.SendKeys('{Enter}')
Instead of Application(), use Desktop() as shown below.
print_control_identifiers() will print identifiers for controls and its descendants. You can use appropriate control from the output to upload your file.
from pywinauto import Desktop
app=Desktop().window(title="Open")
app.print_control_identifiers()

Python Jupyter: Shortcut to copy output of a cell

Please see attached screenshot:
In Jupyter Python: Is there a shortcut to copy the output of a cell to clipboard? (ie without having to manually select and ctrl-c?)
Alternatively is there a python function that instead of print would return its output directly in the clipboard for it to be pasted later?
You may use the following piece of code:
import pandas as pd
df = pd.DataFrame(['Copy me to clipboard'])
df.to_clipboard(index=False,header=False)
what i have done is ---file -->Print Preview , it opens the whole output in another tab than you can copy whatever you want

Categories

Resources