0

Question

I want to add a custom bookmarklet (bookmark that is pure JavaScript code) to a webdriver so it appears in the bookmarks toolbar. This can either be at creation time via add_extension, or after page load.

How can I add this bookmarklet to bookamrks toolbar WITHOUT user interaction, and WITHOUT loading a profile? Any ideas welcomed

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()
"""
  • Why? What are you trying to accomplish by adding this bookmark? – DMart Dec 02 '20 at 04:01
  • I want to add a bookmarklet, the bookmarklet contains javascript code that the user can click to do “X”. its a long story as to why. @DMart –  Dec 02 '20 at 04:06
  • How are you using selenium? What is your ultimate goal? – DMart Dec 02 '20 at 04:28

2 Answers2

0

You can execute javascript arbitrarly in selenium. You don't need a book mark to do this. See this example:

driver.execute_script("return true")

And this answer: https://stackoverflow.com/a/5585345/1387701

DMart
  • 2,401
  • 1
  • 14
  • 19
  • I should’ve specified im aware of this, but im using selenium for an odd use case. I dont want to auto run js. I want users to click on bookmarks. For example one bookmarklet i want to make is a table of instructions that is shown to a user om the current page when they click the instructions bookmark. @DMart –  Dec 02 '20 at 04:21
  • What users? If you're using selenium you're using code to control the browser. – DMart Dec 02 '20 at 04:23
  • Sounds like you might want to create a browser extension instead. – DMart Dec 02 '20 at 04:24
  • Im making a program that uses selenium, yes im looking at apps now :) –  Dec 02 '20 at 04:27
0

Well I figured out 2 ways to do this and ultimately its not worth it lol. I will edit my comment when home to include how