首页 > 解决方案 > 在这种情况下,如何使用动作链模拟 Selenium、Python 的击键?

问题描述

当我在 Windows/Mac 上的任意位置按下 CTRL+SHIFT+2 时,我会打开一个弹出的(PhraseExpress)表单。我想在 Selenium 中模仿它。理想情况下,它应该适用于 Windows 和 Mac。现在我尝试使用 send_keys 和 ActionChains 让它在 Windows 上工作。

def open_form() -> None:
    """function to open the corresponding phraseexpress form (with a hotkey)."""
    bodyElement = WebDriverWait(driver, 40).until(
            EC.element_to_be_clickable((By.TAG_NAME, "body"))) #get a random element to perform send-keys action on.

    # create action chain object
    actions = ActionChains(driver)

  if fullstring == "Note 2":
        if platform == "win32": #if on Windows
            actions.key_down(Keys.CONTROL).key_down(Keys.SHIFT).send_keys_to_element(bodyElement, "2").key_up(Keys.SHIFT).key_up(Keys.CONTROL).perform()
            #actions.key_down(Keys.CONTROL).key_down(Keys.SHIFT).send_keys('2').key_up(Keys.SHIFT).key_up(Keys.CONTROL).perform() # I tried this as well
            #pyautogui.hotkey('ctrl', 'shift', '2') #This is working but I would like to use the above method instead, hoping it will work on Mac as well. This hotkey does not work on Mac. I think the hotkey is executed in Terminal instead of the OS, hence nothing happens. 
        print("You opened PhraseExpress for Note 2")
 

终端中的输出是“您打开了 PhraseExpress for Note 2”,但没有弹出表单。按键不会触发操作系统上的表单。知道可以做什么吗?当 pyautogui.hotkey() 起作用时,为什么此操作不起作用?

标签: pythonselenium

解决方案


推荐阅读