Issues to write a file path on Windows with PyAutoGUI - python

I am writing a simple Python script that goes on a website (with Selenium) and upload a file on the website. I'm using PyAutoGUI to enter the filename and press "Enter" because the website doesn't use an input.
driver.get("https://website_url.com/upload/")
elm = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "file_Picker")))
driver.find_element(By.CLASS_NAME, "file_Picker").click()
pyautogui.write("C:\\Users\\Lulucmy\\PythonProject\\test.png")
pyautogui.press('enter')
time.sleep(2)
The issue is that each time PyAutoGUI write on the upload window, the colon is replaced by a slash :
C/\\Users\\Lulucmy\\PythonProject\\test.png
What I've tried :
Replacing pyautogui.write by pyautogui.typewrite
Using pyautogui.press(':') and dividing the file path in two parts
I think the issue comes from the keyboard layout, but I couldn't find how to change it on PyAutoGUI. Also, if you could think of a solution without using PyAutoGUI I'd be glad to know it.
Thank you for your help!

No, you don't click on the filePicker. Just send the path of the file and it will work:
driver.get("https://website_url.com/upload/")
elm = WebDriverWait(driver, 20).until(EC.element_to_be_clickable
((By.CLASS_NAME, "file_Picker")))
elm.SendKeys("C:\\Users\\Lulucmy\\PythonProject\\test.png")
time.sleep(2)

I found an easy way to fix it - but if you have a "cleaner way" to solve the issue I'm happy to hear it.
The problem came from my AZERTY keayboard; PyAutoGUI seems to use the QWERTY layout by default. I used pyperclip to copy the path in the clipboard, and then paste it (ctrl + v) using .hotkey with PyAutoGUI:
import pyperclip
driver.get("https://website_url.com/upload/")
elm = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "file_Picker")))
driver.find_element(By.CLASS_NAME, "file_Picker").click()
pyperclip.copy("C:\\Users\\Lulucmy\\PythonProject\\test.png")
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')

Related

Pyautogui library is not working properly on CMD program

I am trying to automate a CMD program. I already opened on screen keyboard and "+" sign is located at (1874,919) location.
In the code shown here, I just simply move my cursor towards that location and click on the + sign. But it is not happening as per my expectation. Because according to my expectation when I press + sign it should open a window, but it is not opening and program does not throw any error, it just moves on to the next step.
This is my code:
pa.moveTo(1874,919, duration = 1) # move cursor towarsds + sign.
time.sleep(1)
pa.click(1874,919) # clicking on + plus.
A window should open in that CMD program. By the way I tried the other library "pydirectinput" also.
So basically another library worked for me. Keyboard library.
keyboard.press('+')
this line of code worked for me.
Thanks everyone.

In selenium the send_keys function ignores spaces when used with VNC

Why is the send_keys function ignoring spaces in my python script? I used vnc on ubuntu/debian 10. Everything works correctly when I run the script on my computer, but all spaces disappear on vps with vnc. Error is in Google chrome.
`
element.send_keys("1 2 3")
result: "123"
`
Replacing the spaces with "Keys.SPACE" did not help me.
I tried adding two slashes element.send_keys("John\\ Doe")
Try importing the libs and instantiate actions:
# Needed libs
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
# We create the driver
driver = webdriver.Chrome()
action = ActionChains(driver)
Then make click into your element with something like:
element.click()
Then send the keys like:
action.send_keys(departure).perform()
I haven't been able to get this to work in Chrome. But spaces work fine in Firefox, so I'll have to use that. If someone finds the cause or solution to my problem, please write

Print element copied with the send_keys (Keys.CONTROL, "v")

How do I display a text that I copied with the function send_keys (Keys.CONTROL, "v") to display it with the print command?
Try to get an input and save it to a variable.
Like this:
your_text = driver.find_element_by_id("my_id").get_attribute("value")
print(your_text)
No, you can't do this with selenium alone. Selenium is a browser control and the clipboard is a system level process. You would need to involve other software to properly manipulate the clipboard.
See alos this answer for confirmation of the above statements.

I can't write arabic letters in pyautogui

I was trying to make a spam bot using python but it doesn't work when I try to make it arabic but it works perfectly when I make it english I really need help quickly
This is the code
time.sleep(5)
f = open('spam.txt','r')
fa = f.encode("utf-8")
for word in fa:
pyautogui.typewrite(word)
pyautogui.press("enter")
spam.txt
مرحبا
Thanks in advance
Pyautogui, I'm think, works of your keyboard and there for can't recognise some characters perhaps, I'm not sure but you can send it through a copy paste command like so.
import pyautogui
import pyperclip
import time
time.sleep(5)
# Store our string to the clipboard
pyperclip.copy("مرحبا")
# Hotkey the paste command
pyautogui.hotkey("ctrl", "v")
Tested with into a text document and it is sending the characters just fine although it may depend on the app as to weather it will display left to right or right to left. From what I understand there are characters in Unicode that tell it what side to display if you need to.
For a spam bot put it into a text document read it then put each line in your clipboard.
unfortunately, the Pyautogui doesn't support Arabic or Persian languages, for solve this problem you can type a word in TextPad then copy this manually and use hotkey to do this
from pyautogui import click, press, hotkey
# text
def type():
click(1000,701)
hotkey("ctrl","v")
press("enter")
sleep(1)
you can use pywinauto library instead. It works perfectly.
Try this:
from pywinauto.keyboard import send_keys
time.sleep(1)
send_keys(' سلام {ENTER 2}some more textt{BACKSPACE}', with_spaces=True)
remeber to add this library with
pip install pywinauto

Keyboard shortcuts do not work in Selenium, Firefox, Python

Whether this is opening a tab, saving a bookmark, printing a file or whatever, Selenium can't register key presses. I've tried the following approaches:
1. driver.find_element_by_tag_name("body").send_keys([insert key here])
2. ActionChains(driver).send_keys([insert key here]).perform()
3. ActionChains(driver).key_down(Keys.CONTROL).send_keys([insert key here]).key_up(Keys.CONTROL).perform()
I've also tried putting driver.find_element_by_tag_name('body').click() in front of the each of those lines to force the browser to focus on the page, but even this doesn't work.
THank you in advance for your help.
driver = webdriver.Firefox()
driver.get("https://www.google.com")
input= driver.find_element_by_xpath('//input[#title="Search"]')
input.send_keys("hi this is a test")
input.send_keys(Keys.CONTROL+"a")
input.send_keys(Keys.CONTROL+"x")
time.sleep(5)
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys("v").key_up(Keys.CONTROL).perform()
time.sleep(5)
This code types something on the google search field , selects all, cuts the text and again paste it back .
It works fine , please add the code you tried and the website where you are trying it

Categories