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()
CodePudding user response:
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.
CodePudding user response:
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()