gpt4 book ai didi

Python - Selenium - 如何使用浏览器快捷方式

转载 作者:太空狗 更新时间:2023-10-30 00:17:29 25 4
gpt4 key购买 nike

加载浏览器页面后,我希望使用 Goggle Chrome 中的 CRTL+P 快捷方式进入打印页面,然后只需按回车键即可打印页面。

import time
from selenium import webdriver

# Initialise the webdriver
chromeOps=webdriver.ChromeOptions()
chromeOps._binary_location = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
chromeOps._arguments = ["--enable-internal-flash"]
browser = webdriver.Chrome("C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe", port=4445, chrome_options=chromeOps)
time.sleep(3)

# Login to Webpage
browser.get('www.webpage.com')

我的问题是如何将 key 发送到浏览器本身而不是元​​素?

失败的尝试:将 html 主体分配给作为元素并将键发送给该元素-

elem = browser.find_element_by_xpath("/html/body") # href link
elem.send_keys(Keys.CONTROL + "P") # Will open a second tab
time.sleep(3)
elem.send_keys(Keys.RETURN)

最佳答案

我已经在 Google Chrome 上对此进行了测试,可以使用 ActionChains 类的 .key_down().send_keys() 方法的组合来解决该问题。

ActionChains(driver).key_down(Keys.CONTROL).send_keys('p').key_up(Keys.CONTROL).perform()
ActionChains(driver).send_keys(Keys.ENTER)

关于Python - Selenium - 如何使用浏览器快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21905946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com