加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 21:31:27 所属栏目:Python 来源:网络整理
导读:加载浏览器页面后,我希望使用Goggle Chrome中的CRTL P快捷键进入打印页面,然后只需按返回即可打印页面. import timefrom selenium import webdriver# Initialise the webdriverchromeOps=webdriver.ChromeOptions()chromeOps._binary_location = "C:Progra
加载浏览器页面后,我希望使用Goggle Chrome中的CRTL P快捷键进入打印页面,然后只需按返回即可打印页面.
import time
from selenium import webdriver

# Initialise the webdriver
chromeOps=webdriver.ChromeOptions()
chromeOps._binary_location = "C:Program FilesGoogleChromeApplicationchrome.exe"
chromeOps._arguments = ["--enable-internal-flash"]
browser = webdriver.Chrome("C:Program FilesGoogleChromeApplicationchromedriver.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)

解决方法

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' )

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读