【发布时间】:2014-03-21 06:52:12
【问题描述】:
加载浏览器页面后,我希望使用 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')
我的问题是如何将密钥发送到浏览器本身而不是元素?
尝试失败:将 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)
【问题讨论】:
-
“通常”的方法是照你做,将
<body>元素和.send_keys()定位到该元素。显然,这对您不起作用,但是发生了什么或没有发生什么?如果打开第二个选项卡,您是否尝试过切换到该选项卡然后.send_keys(Keys.RETURN)? -
.send_keys() 到正文不起作用。我没有显示打印预览窗格。
-
啊你的代码注释让我很困惑,我以为你的意思是它正在打开第二个标签。
-
如果要启动 CRTL+t 来打开一个新选项卡,那么 CRTL+p(我想要的)将在同一级别上工作。如果可以让 crtl+t 工作,那么相同的代码将支持 crtl+p。
标签: python selenium browser python-3.x printing