Python-Selenium-如何使用浏览器快捷方式 [英] Python - Selenium - How to use Browser Shortcuts
本文介绍了Python-Selenium-如何使用浏览器快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
加载浏览器页面后,我正在使用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上对此进行了测试,可以使用解决该问题ActionChains类的 .key_down()
和 .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屋!
查看全文