I need to Open a new tab and Paste a URL to the Url line at the browser, using Python+Selenium. Doesn't work with Keys for some reason

Viewed 256

So I have a Copy Link button at the system I'm testing, and that works. means if I paste it manually (ctrl+v) to the browser's url line , it's ok. But when I try using Keys nothing happens.

I need to OPEN a new tab, NAVIGATE to it, DELETE the about:blank (from the browser's url line), PASTE the URL, (that's already been copied), and press ENTER

Here is the code: 1)I open a new tab 2)I navigate to the tab

the first 2 actions work well. the rest don't seem to work for some reason (from this line: action.key_down(Keys.CONTROL).send_keys('a').perform() )

Code:

self.driver.execute_script('''window.open("","");''')
action = ActionChains(self.driver)
profile_window = self.driver.window_handles[1]
self.driver.switch_to.window(profile_window)

action.key_down(Keys.CONTROL).send_keys('a').perform()
action.send_keys(Keys.DELETE).perform()
action.send_keys(Keys.CONTROL).send_keys('v').perform()
action.key_up(Keys.CONTROL).perform()
action.send_keys(Keys.ENTER)
3 Answers

You have to press down before and release CONTROL button before and after sending a and sending v keys as following

action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL)perform()
action.send_keys(Keys.DELETE).perform()
action.send_keys(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
action.send_keys(Keys.ENTER)

Your original code simulates pressing CONTROL, then sending a key, then pressing DELETE while CONTROL is still pressed, then again press CONTROL button (it was already pressed down) and send v button and only now release the CONTROL button and then send ENTER key.
The CONTROL+A or CONTROL+V actions should be: press and hold (key_down) CONTROL, press A (or any other key) release CONTROL button.
If the above still not working you can add short delays between the actions, like this:

action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL)perform()
time.sleep(0.05)
action.send_keys(Keys.DELETE).perform()
time.sleep(0.1)
action.send_keys(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
time.sleep(0.05)
action.send_keys(Keys.ENTER)

Update 1 :

driver.get("https://www.google.com")
driver.execute_script("window.open('about:blank','_blank');")
all_handles = driver.window_handles
driver.switch_to.window(all_handles[1])

driver.get("https://www.amazon.com")

Update 2 :

You can use pyperclip to paste as well. see below

import pyperclip
action = ActionChains(driver)
action.send_keys(pyperclip.paste()).perform()

Ok so, if you want to paste a url (that's kept in the clipboard )to a new tab, use this: It will perform the following:

  1. create a new blank tab
  2. switch to that that
  3. paste the url link that is kept in the clipboard (using pyperclip.paste())

Code :

action = ActionChains(self.driver)
self.driver.execute_script("window.open('about:blank','_blank');")
new_tab = self.driver.window_handles[1]
self.driver.switch_to.window(new_tab)
self.driver.get(pyperclip.paste()) 
Related