Rayrun
← Back to Discord Forum

Hi guys, I need help to convert this code to Playwright Python.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import multiprocessing
import time
import os

path = os.path.dirname(os.path.abspath(__file__))

class Auto:
    def __init__(self):
        self.countopen = 0
        self.position = 0
    def GetDriver(self):
        options = Options()
        options.add_argument('-app=https://bing.com/')
        driver = webdriver.Chrome(service=Service(path + '//chromedriver.exe'), options=options)
        driver.set_window_size(width=312, height=570)
        driver.set_window_position(x=self.position, y=0)
        return driver
    def run(self):
        driver = self.GetDriver()
        driver.get('https://bing.com/')
        driver.find_element(By.NAME, "q").send_keys("Google")
        driver.find_element(By.NAME, "q").send_keys(Keys.ENTER)
        
        time.sleep(20)
        driver.quit()

def driverrun(max):
    r = Auto()
    start_time = time.time()
    processes = []
    for i in range(max):
        p = multiprocessing.Process(target=r.run)
        processes.append(p)
        # print(r.countopen)
        r.countopen += 1
        # print(r.countopen)
        if r.countopen > 1:
            r.position += 312
            # print(r.position)
        p.start()
        
    for process in processes:
        process.join()
    
    print("--- %s seconds ---" % (time.time() - start_time))
    
if __name__ == '__main__':
   driverrun(4)
Screenshot_68.png

This thread is trying to answer question "How can I convert this Selenium Python code to Playwright Python?"

3 replies

chatGpt...?

@dand33: The problem is in "args --app"

The problem is in "args --app"

bandito9274
bandito9274

According to ChatGPT

import pytest from playwright.sync_api import sync_playwright

class Auto: def init(self): self.countopen = 0 self.position = 0

def run(self):
    with sync_playwright() as playwright:
        chromium = playwright.chromium
        browser = chromium.launch()
        page = browser.new_page()
        page.set_viewport_size(width=312, height=570)
        page.set_window_position(x=self.position, y=0)
        
        page.goto('https://bing.com/')
        page.fill('input[name="q"]', 'Google')
        page.press('input[name="q"]', 'Enter')
        
        playwright.stop()

@pytest.fixture(scope="function") def auto(): return Auto()

def test_run(auto): processes = [] for i in range(4): p = multiprocessing.Process(target=auto.run) processes.append(p) auto.countopen += 1 if auto.countopen > 1: auto.position += 312 p.start()

for process in processes:
    process.join()

if name == 'main': pytest.main(['-s'])

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].