首页 > 解决方案 > Webdriver 在创建驱动程序时将书签添加到驱动程序,或将书签保存到书签 - 无需加载配置文件

问题描述

问题

我想将自定义小书签(纯 JavaScript 代码的书签)添加到 Web 驱动程序,以便它出现在书签工具栏中。这可以在创建时通过 add_extension 进行,也可以在页面加载后进行。

如何在没有用户交互且不加载配置文件的情况下将此小书签添加到 bookamrks 工具栏?欢迎任何想法

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_extension(r'C:\path\to\extension.crx')

# Have tried the below but doesn't open the add page to bookmarks?
"""actions = ActionChains(driver)
actions.send_keys(Keys.CONTROL + 'd')
actions.perform()
"""

标签: javascriptpythonseleniumgoogle-chromebookmarklet

解决方案


您可以在 selenium 中任意执行 javascript。您不需要书签即可执行此操作。看这个例子:

driver.execute_script("return true")

而这个答案: https ://stackoverflow.com/a/5585345/1387701


推荐阅读