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
Related
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
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 want to create a script that open links automatically.
But i want the program to open the link in a one page pop up..
i tried to open two links one by one in the same tab, but it always open it in a new Tab in the same Browser.
this is the code:
import webbrowser
import time
search = input("Enter URL Here : ")
search1 = input("Enter Other URL Here : ")
min = int(input("Enter Wait Time : "))
webbrowser.open(search)
time.sleep(min)
webbrowser.open(search1, new=0)
How can I get the URL to be opened in the same Tab as it should be using new=0?
After reading some documentation, the Webbrowser module lacks what you asked for.
BUT, seems like Selenium would work for you. I did some research and I ended up in this Reddit post. You would need to install the module plus the driver of the browser you want to use. If you wanna try, this code snippet should work.
from selenium import webdriver
link1="https://www.example.com"
link2="https://www.google.com/"
driver=webdriver.Chrome()
driver.get(link1)
driver.get(link2)
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 have a selenium webautomation program for tradingview. It all stays in one tab but when I open a new tab/link which it than switches to and which I than want to close after that, it closes the original tab. It looks like this:
for i in range(1, 1001):
input_ = '/html/body/div[8]/div/div[4]/table/tbody/tr[x]/td[1]/div/div[2]/a'
list_input_ = list(input_)
list_input_[44] = str(i)
input_ = ''.join(list_input_)
stock = driver.find_element_by_xpath(input_)
stock.click()
time.sleep(5)
driver.close()
print(input_)
if i > 9:
break
The stock = driver.find_element_by_xpath(input_) and stock.click() opens the new tab/link to the left. But than driver.close() closes the tab I was previously in. I want it close the tab that it just opened and that I'm currently in. Which it supposed to do. It also seems like that none of the selenium commands work on this newly opened tab and only work on the original tab.
The copied element of the link it opens looks like this:<a class="tv-screener__symbol apply-common-tooltip" href="https://www.tradingview.com/symbols/NASDAQ-STAF/" target="_blank" rel="noopener">STAF</a>
Did I do something wrong here? Thanks in advance
It is because you haven't switched focus to the new tab that you have opened. To switch focus to the newly opened tab, use this line:
driver.switch_to.window(driver.window_handles[1])
This line switches the focus to the 2nd tab that is open your browser. Then, you can perform the operations that you want in this tab. Then if you want to close this tab, then just use:
driver.close()
This would close the 2nd tab (the tab that is currently in focus).