Joseph Biju Cadavil
Joseph Biju Cadavil

Reputation: 313

Access data from a chrome extension

I have a website which is loaded using python selenium + chrome web driver for an automation purpose. A chrome extension is running which takes a particular data (say 'x') from the HTML source displays the output (say 'y') in a new tab in the chrome dev-tools.

Is there any way to access this data ('y') using python selenium + chrome web driver, so that the transformation from x->y need not be done in the python side, rather get the data directly out from the chrome extension. Thank You

Upvotes: 2

Views: 1701

Answers (1)

Moshe Slavin
Moshe Slavin

Reputation: 5204

So I understand you have the extension that does the job and you need the already parsed HTML with your custom tags to be tested or scraped with selenium.

The solution:

Start with chrome options to add your extention:

import os
from selenium.webdriver.chrome.options import Options

executable_path = "path_to_webdriver"

chrome_options = Options()
chrome_options.add_extension('path_to_extension')

Then start your chrome driver and go to your site:

from selenium import webdriver

driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)
driver.get("http://your.website.com")

Now you need to switch to the second tab with the formatted HTML:

driver.switch_to.window(driver.window_handles[1])

After switching to the new tab you just go and have fun with selenium!!!

Hope this helps!

Upvotes: 1

Related Questions