What to press ctrl +f on a page in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

We can perform the action of pressing ctrl+f keys in Selenium. There are multiple special Keys available that enable the act of pressing keys via a keyboard like ctrl+c, ctrl+v, ctrl+f and many more. These special Keys are a part of selenium.webdriver.common.keys.Keys class.

key_up() – This method releases a modifier key. The key_up() method is a part of Action Chains class and used to release a key pressed via key_down(). This method is widely used for coping and pasting actions via (ctrl+c, ctrl+v).

In order to carry out this action, we need to first press the ctrl key downwards and simultaneously press the F key. These two steps can be automated by key_up() method and can only be used along with Shift, Alt and Control keys.


key_up(args1, args2)

Here args1 is the key to be sent. The key is defined in Keys class.

The args2 parameter is the element to send keys. If omitted, it shall send a key to the element which is presently in focus.


Code Implementation to press ctrl+f.

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then
#invoke actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
#get method to launch the URL
#to refresh the browser
# action chain object creation
a = ActionChains(driver)
# perform the ctrl+f pressing action
#to close the browser
Published on 29-Jul-2020 14:10:07