1

I want to load multiple chrome browsers using Kameleo's Selenium. Can someone give me some example code to show how it’s done? The selenium portion of my code is:

options = webdriver.ChromeOptions()
    options.add_experimental_option('kameleo:profileId', profile.id)
    driver = webdriver.Remote(
        command_executor=f'{kameleoBaseUrl}/webdriver',
        options=options
    )

    driver.get('https://www.youtube.com/watch?v=wLTp9ni9mKc&t=1s')
    driver.find_element(By.XPATH, "//button[@aria-label='Play']").click()
Tomi
  • 3,370
  • 1
  • 16
  • 26
  • I see that you are not just trying to make Selenium work but you are experimenting with Kameleo as well. I think Kameleo is a powerful tool when it comes to automating several browser instances with different IP address and device fingerprint thank to their Selenium Stealth WebDriver. I would like to raise your attention on this [example](https://github.com/kameleo-io/local-api-examples/blob/master/python/connect_to_selenium/app.py) Simply duplicate from line 16 to line 45 and you will have 2 separate browsers. Let me know if you need some more help and then I'll write a short POC in answer – Tomi Apr 11 '22 at 15:29
  • I would like the browsers to open up at the same time is this possible? – sean froelich Apr 11 '22 at 15:56
  • I have created a code snippet below in [my answer](https://stackoverflow.com/a/71857420/4080226). Let me know if it works. – Tomi Apr 13 '22 at 12:23

2 Answers2

2

I see that you are trying to operate with Kameleo's Selenium Stealth Driver from your python code. And you are trying to perform the same action parallelly from 2 browsers. Here I created a code snippet that will work by using threading

from kameleo.local_api_client.kameleo_local_api_client import KameleoLocalApiClient
from kameleo.local_api_client.builder_for_create_profile import BuilderForCreateProfile
from selenium import webdriver
from selenium.webdriver.common.by import By
from threading import Thread

kameleo_port = 5050
client = KameleoLocalApiClient(f'http://localhost:{kameleo_port}')


def run_browser():
    base_profiles = client.search_base_profiles(
        device_type='desktop',
        browser_product='chrome'
    )

    # This line may be added as well after .set_recommended_defaults() \ line:
    # .set_proxy('socks5', Server(host='<proxy_host>', port=1080, id='<username>', secret='<password>')) \
    create_profile_request = BuilderForCreateProfile \
        .for_base_profile(base_profiles[0].id) \
        .set_recommended_defaults() \
        .build()

    profile = client.create_profile(body=create_profile_request)

    client.start_profile(profile.id)

    options = webdriver.ChromeOptions()
    options.add_experimental_option("kameleo:profileId", profile.id)
    driver = webdriver.Remote(
        command_executor=f'http://localhost:{kameleo_port}/webdriver',
        options=options
    )

    driver.get('https://www.youtube.com/watch?v=wLTp9ni9mKc&t=1s')
    driver.find_element(By.XPATH, "//button[@aria-label='Play']").click()


if __name__ == '__main__':
    Thread(target=run_browser).start()
    Thread(target=run_browser).start()

This code is forcing Kameleo through its Local API to start 2 browsers parallelly. Each of these browsers is started in an isolated environment, so their browser fingerprint is different, so websites will see them as 2 different devices. Once the browsers are started they can be controlled with Selenium commands.

Consider setting up a proxy for the virtual browser profile builder

.set_proxy('socks5', Server(host='<proxy_host>', port=1080, id='<username>', secret='<password>')) \

This will help you to have different IP address for each browsers started by Kameleo.

I used this tutorial to get started with Kameleo and used my Python knowledge for browser automation. Here you can find more info about running functions at the same time in Python.

Tomi
  • 3,370
  • 1
  • 16
  • 26
0

If you need to do the same things multiple time, just use the loop. For example:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option('kameleo:profileId', profile.id)
n=2 # set the number of browsers
for i in range(2):
    driver = webdriver.Remote(
    command_executor=f'{kameleoBaseUrl}/webdriver',
    options=options)
    url='https://app.powerbi.com/view?r=eyJrIjoiNjIwNzg5NzQtNzRlYS00YzFmLWJiNTUtOTM2MGEwY2FjOGJlIiwidCI6ImE3NWRkYWZlLWQ2MmYtNGIxOS04NThhLTllYzFhYjI1NDdkNCIsImMiOjl9'
    driver.get(url)
    driver.find_element(By.XPATH, "//button[@aria-label='Play']").click()