Using Kameleo with Selenium framework

  • Created

When you are working with Kameleo and Selenium:

  1. You will use Kameleo Local API to manage your virtual browser profiles and launch Kameleo's built-in browsers.
  2. You will use Selenium to drive the browser with the W3C protocol.

Automate any browsing activity with Selenium. We'll help with keeping your scripts undetected by the sites you're visiting so that they won't see automation tools in use. Let's see how.

Before reading this article, we recommend reading the Getting started with Kameleo Automation article.

Install Selenium

To work with the Selenium framework, you need to install its official library.

  • npm i selenium-webdriver
  • Install-Package Selenium.WebDriver -Version 4.1.0
  • pip install selenium

Connect Selenium to a Kameleo profile

  1. Follow this guide for starting a profile from your script. Once the profile is up and running, you can move on to the next step to control it with Selenium commands.
  2. You need to build a WebDriver instance and pass the profile's unique ID as a capability named kameleo:profileId. In the case of some Selenium client library implementations, you also have to set the browserName capability, but the value can be anything.
    • const { Builder } = require('selenium-webdriver');
      
      const builder = new Builder()
        .usingServer(`http://localhost:${kameleoPort}/webdriver`)
        .withCapabilities({
          'kameleo:profileId': profile.id,
          browserName: 'Kameleo',
        });
      const webdriver = await builder.build();
    • using OpenQA.Selenium;
      using OpenQA.Selenium.Chrome;
      using OpenQA.Selenium.Remote;
      
      var uri = new Uri($"http://localhost:{KameleoPort}/webdriver");
      var opts = new ChromeOptions();
      opts.AddAdditionalOption("kameleo:profileId", profile.Id.ToString());
      var webdriver = new RemoteWebDriver(uri, opts);
    • from selenium import webdriver
      
      options = webdriver.ChromeOptions()
      options.add_experimental_option("kameleo:profileId", profile.id)
      driver = webdriver.Remote(
        command_executor=f'http://localhost:{kameleo_port}/webdriver',
        options=options
      )
  3. Then you can use any WebDriver command to drive the browser, for example:
    • await webdriver.get("https://google.com");
    • webdriver.Navigate().GoToUrl("https://google.com");
    • driver.get('https://google.com')
  4. Make sure to stop the browser. The virtual browser profile is then stored in you local workspace. Optionally you can export it to a .kameleo file, so you can load it later on another computer as well. Or simply you can delete the profile from Kameleo's workspace once you don't need it anymore. This way, you can keep your resource usage low.
    • await client.stopProfile(profile.id);
      await client.exportProfile(profile.id, { body: { path: `${__dirname}\\test.kameleo` } }); // optional
      await client.deleteProfile(profile.id); // optional
    • await client.StopProfileAsync(profile.Id);
      await client.ExportProfileAsync(profile.Id, new SaveProfileRequest(
        Path.Combine(Environment.CurrentDirectory,"test.kameleo"))); // optional
      await client.DeleteProfileAsync(profile.Id); // optional
      
    • client.stop_profile(profile.id)
      path = f'{os.path.dirname(os.path.realpath(__file__))}\\test.kameleo'
      result = client.export_profile(profile.id, body=SaveProfileRequest(path=path)) # optional
      client.delete_profile(profile.id); # optional

If you want to see the full example code, click any of the links below:

Was this article helpful?

2 out of 5 found this helpful