具有 python 动作链的硒无法正常工作

selenium with python action chains not working

我正在尝试向我的 selenium chrome-驱动程序发送 ALT+ESC 命令,以将其发送到所有其他 windows

的后面

这是相关代码

from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.send_keys(Keys.LEFT_ALT, Keys.ESCAPE)
actions.perform()

这不起作用请帮助

在创建浏览器会话并打开它之前,您不能将密钥发送到浏览器。
您首先必须初始化驱动程序,打开一些网页,让页面完全加载,然后才尝试将这些密钥发送到网页。
您的代码可能是这样的:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome(executable_path='chromedriver.exe')

wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

driver.get("https://www.some_url.com/")

wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='some class name']")))

actions.send_keys(Keys.LEFT_ALT, Keys.ESCAPE)
actions.perform()

按key-combo:

from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains

ActionChains(driver).key_down(Keys.LEFT_ALT).send_keys(Keys.ESCAPE).key_up(Keys.LEFT_ALT).perform()

但似乎这是一个 OS-level 键组合,可以与 windows 一起使用,这在 selenium 上下文中不起作用。

应用于网页元素的 Selenium 操作,在浏览器中触发一些事件。

最终我找不到在 selenium 中执行 OS 命令的方法。

以下代码具有相同的功能,但不一定运行在浏览器上

from pyautogui import hotkey
hotkey('altleft', 'esc')