2014-02-20 50 views
1

一旦浏览器页面加载我期待在使用Chrome的风镜的CRTL + P快捷键,进入打印页面,然后只需按返回打印页面。的Python - 硒 - 如何使用浏览器快捷方式

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) 
+0

的“通常”的方法是做你是,目标在''元件和'.send_keys()'到。显然,这不适合你,但是什么是或不是正在发生?如果第二个选项卡打开,是否尝试更改为该选项卡,然后是'.send_keys(Keys.RETURN)'? –

+0

.send_keys()不正常。我没有得到打印预览窗格。 – Phoenix

+0

啊你的代码评论让我困惑,我以为你的意思是它打开第二个标签。 –

回答

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

ActionChains(browser).send_keys(Keys.CONTROL, "p").perform() 

将发送快捷键的打印对话框

我没有找到了一种在FF中进行打印的方法 - ctrl + p将打开打印对话框,但FF有一个焦点问题,它不允许用户为对话本身执行Keys.ENTER

希望这会为你在Chrome的工作,我没有测试它有

如果你找到解决的办法,请更新 - 可能尝试AutoIt的

如果没有上述工作,你总是可以做

browser.get_screenshot_as_file(path + 'page_image.jpg') 
+0

感谢您的回复,但此代码似乎无法用于我的Goggle Chrome。 – Phoenix

+1

它不起作用。 –

1
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

browser.get('https://www.myglenigan.com/project_search_results.aspx?searchId='+ID) 
element=browser.find_element_by_xpath("//body") 
element.send_keys(Keys.CONTROL, 'p') 

刚一说明,这将打开Firefox的打印面板。但是相同的代码在Goggle Chrome中不起作用。

+1

它不起作用。 –

相关问题