Google Chrome/Firefox和Selenium的击键不适用于Python

编程入门 行业动态 更新时间:2024-10-26 14:34:37
本文介绍了Google Chrome/Firefox和Selenium的击键不适用于Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

运行以下命令:

from selenium import webdriver from selenium.webdrivermon.keys import Keys # Get web driver going cp = webdriver.ChromeOptions() cp.add_argument("--browser.download.folderList=2") cp.add_argument("--browser.helperApps.neverAsk.saveToDisk=image/jpg") cp.add_argument("--browser.helperApps.neverAsk.saveToDisk=image/png") cp.add_argument("--browser.download.dir=~/Downloads/") driver = webdriver.Chrome(chrome_options=cp) driver.get("www.google") # Try to open a new tab driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL, 't')

这是尝试打开新标签页的尝试,但是代码不起作用.尝试使用Firefox时也是如此.对于Firefox,如果我不更改配置文件(使用等效代码),此可以起作用,但是不适用于自定义配置文件.

This was an attempt to open a new tab, but the code does not work. This is also the case when trying to use Firefox. For Firefox, this does work if I don't change the profile (using equivalent code), but does not work with a custom profile.

我也希望能够发送Ctrl+S,但是似乎没有涉及特殊字符的命令(尽管我仍然可以send_keys普通文本,不涉及诸如Ctrl之类的特殊键).

I would also like to be able to send Ctrl+S too, but it seems no commands involving a special character work (though I can still send_keys normal text, not involving special keys like Ctrl).

我该怎么做才能发送Ctrl+T和Ctrl+S(尤其是 后者)?

What can I do to be able to send Ctrl+T and Ctrl+S (especially the latter)?

推荐答案

您可以使用下面给出的操作链.

You can use action chain as given below.

ActionChains(driver).key_down(Keys.CONTROL).send_keys('s').key_up(Keys.CONTROL).perform()

更多推荐

Google Chrome/Firefox和Selenium的击键不适用于Python

本文发布于:2023-11-24 12:20:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625172.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   击键   Chrome   Google   Firefox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!