Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How do I type ESC, ENTER and other keys? #1346

Answered by mdmintz
HellShuro asked this question in Q&A
Discussion options

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

You must be logged in to vote

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

Comment options

image
Or is it only possible in this way?

You must be logged in to vote
0 replies
Comment options

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")
You must be logged in to vote
0 replies
Answer selected by mdmintz
Comment options

@mdmintz But how to press Keys multiple times without specifying element?

Say, i want like this

image

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.

You must be logged in to vote
5 replies
Comment options

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)
Comment options

Using Keys.TAB changes the active element. Then to click the active element, use:

self.click_active_element()
Comment options

image

YOU'RE A GENIOUS!
Thank you so much, hope you have a great day (y)

Comment options

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?

Comment options

@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.

Comment options

hey @mdmintz how can we press keys in sb.cdp?

You must be logged in to vote
5 replies
Comment options

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.

Comment options

I tried sb.cdp.press_keys("//input[@id='gn-search-input'] \n", text) but it does not seem to work

Comment options

The "\n" should go with the text, not the selector. And you should be using CSS selectors with CDP Mode (not XPath).

Comment options

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

Comment options

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /