Home > database >  selenium python need to press option key on mac os
selenium python need to press option key on mac os

Time:07-29

body = driver.find_element(By.TAG_NAME, "body")
body.send_keys(Keys.CONTROL   Keys.OPTION   'f')

output:

    body.send_keys(Keys.CONTROL   Keys.OPTION   'f')
AttributeError: type object 'Keys' has no attribute 'OPTION'

How can I do that?

CodePudding user response:

You should simply use Keys.COMMAND instead of Keys.CONTROL for MacOS, for one thing.

Unfortunately there is no built-in "OPTION" in the Selenium Keys class, but I would try using this code instead: "\u2325".

Finally, I am not sure if you can perform this simply with element.send_keys(). Maybe you can. But I would probably use ActionChains, something like this:

# need this import
from selenium.webdriver.common.action_chains import ActionChains

ActionChains(driver).key_down(Keys.COMMAND).key_down("\u2325").send_keys('f').key_up(Keys.COMMAND).key_up("\u2353").perform()

Could be more readable like this:

action = ActionChains(driver)
action.click(on_element = body) # put focus on desired element
action.key_down(Keys.COMMAND)
action.key_down("\u2325")
action.send_keys('f')
action.key_up(Keys.COMMAND)
action.key_up("\u2325")
action.perform()
action.reset_actions() # Clears actions that are already stored locally and on the remote end
  • Related