Python - Selenium - 如何使用浏览器快捷方式 [英] Python - Selenium - How to use Browser Shortcuts
问题描述
加载浏览器页面后,我希望使用 Goggle Chrome 中的 CRTL+P 快捷方式进入打印页面,然后只需按回车键即可打印页面.
Once a browser page is loaded I'm looking to use the CRTL+P shortcut in Goggle Chrome to enter the print page and then simply press return to print the page.
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')
我的问题是如何将密钥发送到浏览器本身而不是元素?
My question is how do I send keys to the browser itself rather than an element?
尝试失败:将 html 正文指定为元素并向其发送键 -
Failed Attempt: To assign the html body to as the element and send keys to that-
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 上测试过这个问题,使用 .key_down()
和 的组合可以解决这个问题.ActionChains 类的 send_keys()
方法.
I have tested this on Google Chrome and the problem can be solved using a combination of .key_down()
and .send_keys()
methods of the ActionChains class.
ActionChains(driver).key_down(Keys.CONTROL).send_keys('p').key_up(Keys.CONTROL).perform()
ActionChains(driver).send_keys(Keys.ENTER)
这篇关于Python - Selenium - 如何使用浏览器快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!