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

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.

Related

Issues to write a file path on Windows with PyAutoGUI

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')

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

How to send copied text to a text file using pywinauto?

I have copied a text from my software using pywinauto. Unfortunately, I don't know how to paste that to a text file. The following is the code that I wrote:
The last line of the code is not working as it should not. However, that is what I should do. Can anyone help me to solve this problem?
pywinauto.mouse.double_click(button='left', coords=(820,168))
pywinauto.keyboard.send_keys('^c')
f= open("trial.txt","w+")
f.write(pywinauto.keyboard.send_keys('^v'))```
I see that you're trying to paste the contents of clipboard, but there is no visual area to paste.
f.write() will accept text through a variable or, by passing some text. Invoking Ctrl + V is a GUI operation, which can't replace the text in f.write()
You can use pyperclip module to access the clipboard contents.
import pyperclip
"""yourcode"""
f.write(pyperclip.paste())
f.close()
You can also programatically copy something to system clipboard using pyperclip.
pyperclip.copy("This is a text copied to clipboard from Python script!!")
You can now check the contents by invoking Ctrl + V in some GUI application like notepad.
You can try send it hotkey
pyautogui.hotkey('ctrl','v')

Failing to upload a file using Selenium

I'm trying to upload a file to a form using Selenium using this code on eclipse:
search = driver.find_element_by_xpath("//input[#type='file']")
search.send_keys("D:/test.txt")
search.send_keys(Keys.RETURN)
This error keeps showing up:
selenium.common.exceptions.WebDriverException: Message: File not
found: D:/test.txt
The file is in place, where do you think the problem is?
I guess the reason is within the slash used in the path - I think it requires a backslash instead.
What if you try to use search.send_keys("D:\\test.txt")? Not sure if double backslash is required for that, so you can try with single one as well.
EDIT
I tried my own code on simple form with just the input[type=file] and with Submit button:
search = browser.find_element_by_xpath("//input[#type='file']")
search.send_keys("F:\\test.txt")
submit = browser.find_element_by_css_selector("input[type=submit]")
submit.click()
And somehow, it worked just fine, just had to escape backslash and to use Submit button instead of using ENTER button.
So make sure your file is actually there, within the path you posted, and such code (at least on Windows) works just fine. Also, you should make sure you have permission to this file.

Categories