【问题标题】:Selenium on Mac Using Python, how to issue keyboard commandsMac 上的 Selenium 使用 Python,如何发出键盘命令
【发布时间】:2021-06-24 15:43:48
【问题描述】:

我一直在尝试在勇敢的浏览器中使用 python 中的 selenium 发出键盘命令。 我试过这样的事情: body = browser.find_element_by_tag_name("body")

body.send_keys(Keys.COMMAND,"t") 这似乎运行良好,但没有任何效果。我究竟做错了什么?好像我在错误的地方执行命令,但是如果是这样,我应该将它发送到哪里?

【问题讨论】:

  • 我期待它打开一个新标签
  • 语法似乎是:send_keys(Keys.COMMAND + "t")
  • 我找到了解决方法,谢谢。
  • 你能发帖回答吗?
  • 是的,刚刚发布了。

标签: python macos selenium command


【解决方案1】:

我基本上是这样做的:

        from selenium.webdriver.chrome.options import Options 
        import time 
        from selenium.webdriver.common.by import By
        from selenium.webdriver.common.keys import Keys 
  
options = Options()
options.binary_location = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
driver_path = '/Users/[myusername]/Desktop/seleni/chromedriver'

browser = webdriver.Chrome(options = options, executable_path = driver_path)
browser.get('chrome://newtab') #or whatever page you want

#Here's the important part vvv
browser.execute_script("window.open('https://duckduckgo.com/?q=%27&t=brave&ia=answer')")````

Hopefully this helps in some way. Sorry if I'm a bit of a newbie at this kind of stuff.

【讨论】:

    猜你喜欢
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2012-10-28
    • 2021-01-07
    • 2011-03-22
    • 1970-01-01
    相关资源
    最近更新 更多