【问题标题】:Selenium: Python ActionChains doesn't workSelenium:Python ActionChains 不起作用
【发布时间】:2014-10-02 14:42:24
【问题描述】:

我正在尝试在 Chromedriver 中做一个简单的 Control + S

我试过webdriver.send_keys(Keys.CONTROL, "s") 没用

然后我尝试了

ActionChains(driver).key_down(Keys.LEFT_CONTROL).key_down('s').perform()

还是不行。

我还有什么其他选择?当我在 chromedriver 浏览器中手动按 CONTROL + S 时,它可以工作。为什么硒不能做到这一点?

我正在使用 Selenium Python。

【问题讨论】:

  • “没用”的意思是……?

标签: python selenium selenium-webdriver


【解决方案1】:

将密钥发送到Body 元素:

from selenium.webdriver.common.keys import Keys

body = driver.find_element_by_tag_name('body')
body.send_keys(Keys.CONTROL, "s")  # on Mac: body.send_keys(Keys.COMMAND, "s")

或者,通过这种方式获取实际的 html 源代码:

html = driver.find_element_by_xpath("//*").get_attribute("outerHTML")
print html

【讨论】:

  • 我尝试发送到正文,但没有触发保存对话框。我在窗户上
  • 是的,在 Windows 7 上使用 chromedriver。我可能会在 ubuntu 上尝试一下,看看它是否有效
  • @user299709 好的,但是为什么需要启动“保存”对话框?请描述用例。
  • 这样我就可以完整保存页面了。
  • 至少,在 Mac 上,任何类型的组合键都不起作用...我在 Chrome 上尝试了许多组合键,但仍有可能正常工作...
【解决方案2】:
from selenium.webdriver.common.keys import Keys
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("Https://www.google.com")
body = driver.find_element_by_tag_name('body')
body.send_keys(Keys.CONTROL, "s")

上面的代码在 windows+firefox 上为我工作。请确保您使用的是最新版本的 Selenium version 2.45.0

【讨论】:

  • 这与 alecxe 的答案相同(“将事件发送到 body”),但在许多个月后发布。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2020-03-18
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多