您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:20:18 python 字数 2100 阅读 456 来源 www.jb51.cc/python

加载浏览器页面后,我希望使用Goggle Chrome中的CRTL P快捷键进入打印页面,然后只需按返回即可打印页面. import time from selenium import webdriver # Initialise the webdriver chromeOps=webdriver.ChromeOptions() chromeOps._binary_location = "C:\

概述

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

总结

以上是编程之家为你收集整理的Python – Selenium – 如何使用浏览器快捷方式全部内容,希望文章能够帮你解决Python – Selenium – 如何使用浏览器快捷方式所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶