App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide How to Press Enter without Element in Selenium Python?

How to Press Enter without Element in Selenium Python?

By Pooja Deshpande, Community Contributor -

How to press enter in sendKeys?

We can press enter in Selenium with the help of send_keys() function in Selenium Python.

send_keys() function takes different keys as its parameter. Hence we need to import keys before using this function. 

We can perform all the keyboard operations with the help of keys in Selenium.Class selenium.webdriver.common.keys comes with various methods that one can use for this purpose.

For pressing enter we need to pass Keys.ENTER as parameter to send_keys() method as below

from selenium.webdriver.common.keys import Keys

driver.find_element_by_name("Value").send_keys(Keys.ENTER)

Sending a key without an Element

For sending keys without specifying an element in Python we can use the ActionChains class as follows

from selenium.webdriver.common.action_chains import ActionChains

value = “Test”

actions = ActionChains(driver) 

actions.send_keys(value)

actions.perform()

Selenium Press Enter without Element Python

We can also press enter without specifying any particular element in Selenium.

For example, we want to send text to a username field on a login page. The login page has already loaded and the username field is in focus as soon as the login page loads. 

Here we can directly send the username value as the  username text box is in focus using the send_keys method,then press tab to navigate to the password field and press enter on the login button.

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox(executable_path="C:\geckodriver.exe")

driver.get("url")

actions = ActionChains(driver) 

actions.send_keys(value=username)

actions.send_keys(keys.TAB)

actions.send_keys(value=password)

actions.send_keys(keys.ENTER)

actions.perform()

driver.quit()

What to use instead of sendKeys in Selenium?

To send keys  without the use of send_keys() method, we can use Actions class method mentioned above or Javascript executor in Selenium below. 

  • Javascript executor is an alternate way to locate web elements and perform actions on them. 
  • With the help of Javascript, DOM(document object model) has access to all the web elements and hence can perform actions on these elements. 
  • Selenium has the ability to integrate with Javascript in order to locate web elements with the help of Javascript executor. 

There is a method in Selenium called execute_script() which helps to execute Javascript commands. We need to pass these commands as arguments to the execute_script() method.

Below is an example to send keys with the help of Javascript executor     

from selenium import webdriver

driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe")

#Navigate to the webpage

driver.get("https://app.hubspot.com/login/")

            #Locate element 

driver.find_element_by_id("username")

#create java script executor instance

JavascriptExecutor js = (JavascriptExecutor ) driver

#send keys with help of javascript executor

runJS.executeScript("arguments[0].value='abc@gmail.com';", username);

Conclusion

There are multiple ways in which we can use the send_keys() method and it helps to perform various keyboard operations. 

However, ensure to opt for BrowserStack Cloud Selenium Grid of 3000+ real devices and desktop browsers. QA teams can test websites hosted on development environments or behind firewalls with zero setup or configuration. Running your Selenium tests with Python on BrowserStack is simple and effective. 

Start Selenium Testing

Tags
Automation Testing Selenium Webdriver

Featured Articles

How to Read/Write Excel Data using Apache POI Selenium

How to read Config Files in Python using Selenium

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.