keystrokes with Google Chrome/Firefox and Selenium not working in Python

寵の児 提交于 2020-01-03 04:22:29

问题


Run the following:

from selenium import webdriver
from selenium.webdriver.common.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("http://www.google.com")
# Try to open a new tab
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL, 't')

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.

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

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


回答1:


You can use action chain as given below.

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


来源:https://stackoverflow.com/questions/44664044/keystrokes-with-google-chrome-firefox-and-selenium-not-working-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!