-
Notifications
You must be signed in to change notification settings - Fork 1.4k
-
How do I type ESC, ENTER and other keys?
I only see the up and down, left and right approach
Using self.send_Keys(Key.ESCAPE) has no effect
Beta Was this translation helpful? Give feedback.
All reactions
For using special keys, you can use:
from selenium.webdriver.common.keys import Keys ... ... self.send_keys(SELECTOR, Keys.ESCAPE)
But for hitting ENTER/RETURN, you can use \n
. Example:
self.type("input#password", "secret_sauce\n")
Replies: 4 comments 10 replies
-
image
Or is it only possible in this way?
Beta Was this translation helpful? Give feedback.
All reactions
-
For using special keys, you can use:
from selenium.webdriver.common.keys import Keys ... ... self.send_keys(SELECTOR, Keys.ESCAPE)
But for hitting ENTER/RETURN, you can use \n
. Example:
self.type("input#password", "secret_sauce\n")
Beta Was this translation helpful? Give feedback.
All reactions
-
@mdmintz But how to press Keys multiple times without specifying element?
Say, i want like this
But it doesn't work.
I've tried combining with the traditional selenium with actionchains but it just gave me another error, also it opens 2 browsers since i call for driver and self.
Beta Was this translation helpful? Give feedback.
All reactions
-
A selector must always be specified to attach the action to. You can use html
or body
for the page when you don't want to use a specific element on the page.
Example:
from selenium.webdriver.common.keys import Keys self.send_keys("html", Keys.TAB)
Beta Was this translation helpful? Give feedback.
All reactions
-
Using Keys.TAB
changes the active element. Then to click the active element, use:
self.click_active_element()
Beta Was this translation helpful? Give feedback.
All reactions
-
YOU'RE A GENIOUS!
Thank you so much, hope you have a great day (y)
Beta Was this translation helpful? Give feedback.
All reactions
-
🎉 1
-
Is there a self.send_keys_active_element("whatevs")? Or maybe make send_keys have a default selector of active_element? so if it gets 2 strings, first one is selector and second string is text to type but if only 1 string is provided, assume selector is active_element and provided string is text to type?
Beta Was this translation helpful? Give feedback.
All reactions
-
@wackydude: self.send_keys(self.get_active_element_css(), "TEXT")
self.get_active_element_css()
was added this week, so make sure you have the latest SeleniumBase first.
Beta Was this translation helpful? Give feedback.
All reactions
-
hey @mdmintz how can we press keys in sb.cdp
?
Beta Was this translation helpful? Give feedback.
All reactions
-
Depends on which keys. Most keys can be pressed with sb.cdp.type(selector, text)
. There are some working escape sequences such as \n
or \r
for Enter and \b
for Backspace. For other keys like ESC, you may need PyAutoGUI
.
Beta Was this translation helpful? Give feedback.
All reactions
-
I tried sb.cdp.press_keys("//input[@id='gn-search-input'] \n", text)
but it does not seem to work
Beta Was this translation helpful? Give feedback.
All reactions
-
The "\n" should go with the text, not the selector. And you should be using CSS selectors with CDP Mode (not XPath).
Beta Was this translation helpful? Give feedback.
All reactions
-
Tried
germain = "Paris saint germain\n" sb.cdp.press_keys("input#gn-search-input", germain)
but doesn't seem to work. Pardon me if i'm missing something, i'm new to this amazing project. just need to explore it fully
Beta Was this translation helpful? Give feedback.
All reactions
-
If "\n" didn't work, and "\r" didn't work, then use the PyAutoGUI methods for that, eg: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_antibot_login.py
Beta Was this translation helpful? Give feedback.