Write question mark in url - python-3.x

I'm using pyautogui to perform some routines.
I'm trying to write a url that contains '?' but this character is not written in the url, how can I do this?
below my code
import pyautogui as m
#from requests.utils import requote_uri
st = '?'
url = 'http://10.100.0.34/Relatorios/Pages/Report.aspx?
ItemPath=%2fDBM%2fGrafico+Espa%c3%a7o+Servidores'
print(url)
def checklist():
m.moveTo(27,882,duration=1)
m.click(27,882)
m.moveTo(115,269,duration=1)
m.click(115,269)
m.moveTo(128,37,duration=1)
m.click(128,37)
m.typewrite(url,interval=0.02)
m.press('enter')
checklist()
OUTPUT
http://10.100.0.34/Relatorios/Pages/Report.aspxItemPath=%2fDBM%2fGrafico+Espa%c3%a7o+Servidores

I've been trying to recreate the problem for so long but I can't seem to get what's causing the problem cause everything works fine on my pc. try raising the interval or splitting the code into sections like this
typewrite('http://10.100.0.34/Relatorios/Pages/Report.aspx'+'?'+'ItemPath=%2f
DBM%2fGrafico+Espa%c3%a7o+Servidores')
also try adding a time.sleep() before the typewrite function maybe the m.click() is causing the problem.

One way to solve the ? mark problem with pyautogui is using pyperclip library.
import pyautogui
import pyperclip
pyperclip.copy('some link that has ? in it')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')

Related

Type object 'By' has no attribute 'Link_text'

Get tired to guess proper syntax. Please, help me out with my problem! Writing code in Python3.
My code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
url = "https://www.instagram.com/"
driver = webdriver.Chrome("/users/vp/desktop/Python/chromedriver")
try:
driver.get(url)
time.sleep(5)
login = driver.find_element(By.Link_text, 'Forgot password?').click()
finally:
driver.close()
driver.quit()
"Forgot password?" html:
a class="_2Lks6" href="/accounts/password/reset/" tabindex="0" xpath="1">Forgot password? a>
I would change "Link_Text" to "LINK_TEXT" reason being selenium requires it to be in "all caps" or else it won't recognize the By. Method
I assume the original poster has already figured this one out or reverted to the depreciated method. I just wanted to add this answer for anyone else whom may have the same error without realizing the issue instead of being forced to use a depreciated method.
The issue is not a syntax error, but trying to access an attribute that is not there for an object.
you can read about this here: https://docs.python.org/3/library/exceptions.html#TypeError
In your case, By (object) does not have an attribute called Link_text.
I guess supported attributes for By are listed here:
https://www.selenium.dev/selenium/docs/api/py/_modules/selenium/webdriver/common/by.html#By
or
you can do the following to list the attributes
dir(By)

how to put a whole script into a loop?

I have 0 experience in coding but I put my ambition and code something in python for a game. Everything works perfect :D
But I have a problem. I don't know how to infinite repeat the code. I've look over the internet but i didn't understand much.
Im gonna let here a part of the code maybe someone can explain me how to put all code into 'repeat'.
import time
import pyautogui
pyautogui.click(942, 642)
time.sleep(1)
pyautogui.click(807, 581)
time.sleep(1)
Thanks.
Simply wrap it all (or, well, not the imports) in a while True: loop.
import time
import pyautogui
while True:
pyautogui.click(942, 642)
time.sleep(1)
pyautogui.click(807, 581)
time.sleep(1)

how to write an "#" with pyautogui

So basically This is my code:
randnum = random.randint(1, 999999)
randomname = "justalollipop" + str(randnum)
randomemail = randomname + "#gmail.com"
And after that I want to write it into a login field with pyAutoGUI with pyautogui.typewrite(randomemail)
The problem now is, that everytime it tries to write this, it just writes without the #
Edit: I know that # is an operator.
Edit//2:
So I basically looked into pyAutoGUI and found out that the code has to be
pyautogui.typewrite(randomname)
pyautogui.keyDown('altright')
pyautogui.keyDown('q')
pyautogui.keyUp('altright')
pyautogui.keyUp('q')
pyautogui.typewrite("gmail.com")
Instead of just
pyautogui.typewrite(randomemail)
Thank you for the answers
Best answer I've found after many tries:
import pyautogui
import pyperclip
pyperclip.copy("#")
pyautogui.hotkey("ctrl", "v")
As the documentation states
You can only press single-character keys with typewrite(), so you
can’t press the Shift or F1 keys, for example.
Why don't you try:
pyautogui.typewrite(randomemail)
pyautogui.press('#')
pyautogui.typewrite("gmail.com")
P.S. Maybe is just an encoding problem and it's possible to pass the # encoded. But I really don't know the inner internals of pyautogui.
I used to use pywinauto but it only supports windows as the name states.

accessing clipboard via win32clipboard

I am working on a project in which i have to continuouly check clipboard content. If clipboard content matches with certain specified data, then it should be deleted from clipboard.
After doing a lot of googling, I find out that it can be done easily by win32clipboard api.
I am using Python as a programming language.
Following is a code for file(CF_HDROP) format:
import win32clipboard
import win32con
def filecopy():
try:
win32clipboard.OpenClipboard()
print win32clipboard.GetClipboardData(win32con.CF_HDROP)
win32clipboard.CloseClipboard()
except TypeError:
pass
Following is a code for text format:
import win32clipboard
def textcopy():
try:
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
print data
win32clipboard.CloseClipboard()
except TypeError:
pass
I am calling above functions in a infinite loop.
Individual function works correctly. But the problem with win32clipboard is that,
after win32clipboard.OpenClipboard() command, win32clipboard lock the clipboard and only realise it after CloseClipboard() command. In between i cant copy anything in clipboard.
How can i solve this problem??
Any other suggestion are also welcome to achieve ultimate aim.
NOTE: Its not necessary to use python. You can use any other language or any other approach.
An infinite polling loop (especially one without delays) is going to be a problem since there's no way to read the contents without locking. Instead you should look into becoming a Clipboard viewer (pywin32 and msdn), that way you are notified of the clipboard contents change and then you can inspect it (get it and get out). If you google a bit on pywin32 and WM_DRAWCLIPBOARD, you'll find some python implementations.

python 3.3 basic error

I have python 3.3 installed.
i use the example they use on their site:
import urllib.request
response = urllib.request.urlopen('http://python.org/')
html = response.read()
the only thing that happens when I run it is I get this :
======RESTART=========
I know I am a rookie but I figured the example from python's own website should be able to work.
It doesn't. What am I doing wrong?Eventually I want to run this script from the website below. But I think urllib is not going to work as it is on that site. Can someone tell me if the code will work with python3.3???
http://flowingdata.com/2007/07/09/grabbing-weather-underground-data-with-beautifulsoup/
I think I see what's probably going on. You're likely using IDLE, and when it starts a new run of a program, it prints the
======RESTART=========
line to tell you that a fresh program is starting. That means that all the variables currently defined are reset and/or deleted, as appropriate.
Since your program didn't print any output, you didn't see anything.
The two lines I suggested adding were just tests to figure out what was going on, they're not needed in general. [Unless the window itself is automatically closing, which it shouldn't.] But as a rule, if you want to see output, you'll have to print what you're interested in.
Your example works for me. However, I suggest using requests instead of urllib2.
To simplify the example you linked to, it would look like:
from bs4 import BeautifulSoup
import requests
resp = requests.get("http://www.wunderground.com/history/airport/KBUF/2007/12/16/DailyHistory.html")
soup = BeautifulSoup(resp.text)

Resources