need to start playwright in async

This commit is contained in:
2022-03-21 10:36:26 +01:00
parent 0ec824d7cc
commit 68680af13b
+48 -31
View File
@@ -1,7 +1,9 @@
import asyncio
import subprocess
import time
import playwright
from playwright.async_api import async_playwright
from playwright.sync_api import sync_playwright
from pojo.contact_pojo import ContactPojo
@@ -20,51 +22,52 @@ class CommandorPage:
def __init__(self):
pass
def start_page(self, proxy, contact: ContactPojo):
async def start_page(self, proxy, contact: ContactPojo):
# specifying an explicit component name
with sync_playwright() as pwright:
self.browser = pwright.chromium.launch(headless=False, timeout=30000, proxy=proxy)
self.page = self.browser.new_page(
async with async_playwright() as pwright:
self.browser = await pwright.chromium.launch(headless=False, timeout=30000, proxy=proxy)
self.page = await self.browser.new_page(
user_agent="Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Mobile Safari/537.36")
self.page.goto(RDV_URL)
content = self.page.content()
self.setPhoneCountry(self.page)
self.setPhoneNumber(contact.phone)
self.setName(contact.last_name, contact.first_name)
self.setEmail(contact.mail)
self.setIdNumber(contact.passport)
self.checkCgu()
self.clickOnValidBtn()
await self.page.goto(RDV_URL)
content = await self.page.content()
await self.setPhoneCountry()
await self.setPhoneNumber(contact.phone)
await self.setName(contact.last_name, contact.first_name)
await self.setEmail(contact.mail)
await self.setIdNumber(contact.passport)
await self.checkCgu()
await self.clickOnValidBtn()
# page.on('load', self.on_page_loaded)
print(content)
time.sleep(100000)
await asyncio.sleep(1000)
def on_page_loaded(self):
print("page loaded")
def setPhoneCountry(self, page):
page.evaluate("""()=>document.getElementById("phone_country").value = \"FR\" """)
async def setPhoneCountry(self):
await self.page.evaluate("""()=>document.getElementById("phone_country").value = \"FR\" """)
def setPhoneNumber(self, phoneNumber):
self.page.evaluate("""(phoneNumber)=>document.getElementById("phone_number").value =phoneNumber""", phoneNumber)
async def setPhoneNumber(self, phoneNumber):
await self.page.evaluate("""(phoneNumber)=>document.getElementById("phone_number").value =phoneNumber""",
phoneNumber)
def setName(self, lastName, firstName):
self.page.evaluate("""(name)=> {
async def setName(self, lastName, firstName):
await self.page.evaluate("""(name)=> {
document.getElementById("surname").value = name.lastName;
document.getElementById("name").value = name.firstName}""", {'lastName': lastName, 'firstName': firstName})
def setEmail(self, email):
self.page.evaluate("""(email)=>document.getElementById("email").value = email""", email)
async def setEmail(self, email):
await self.page.evaluate("""(email)=>document.getElementById("email").value = email""", email)
def setIdNumber(self, id):
self.page.evaluate(""" (id) => document.getElementById("passport_id").value = id""", id)
async def setIdNumber(self, id):
await self.page.evaluate(""" (id) => document.getElementById("passport_id").value = id""", id)
def checkCgu(self):
self.page.evaluate("""document.getElementById("cgu").checked = true;
async def checkCgu(self):
await self.page.evaluate("""document.getElementById("cgu").checked = true;
document.getElementById("processing").checked = true""")
def clickOnValidBtn(self):
self.page.evaluate("""document.getElementsByClassName("btn")[0].click();""")
async def clickOnValidBtn(self):
await self.page.evaluate("""document.getElementsByClassName("btn")[0].click();""")
def clear_app_data(self):
pass
@@ -91,12 +94,26 @@ class CommandorPage:
time.sleep(10)
async def main():
contact = ContactPojo(phone_number="755667750", passport_number="5123456789", last_name="PAN", first_name="Lei",
mail="panleicim@gmail.com", ccid="", position=0)
page = CommandorPage()
await page.start_page(None, contact)
# task = asyncio.create_task(wait_for_otp())
# await task
async def wait_for_otp():
sec = input("Press Enter to continue...")
time.sleep(20)
print("input otp is: " + sec)
if __name__ == '__main__':
proxy = {
"server": "http://gw.ntnt.io:5959",
"username": "panleicim-cc-any-sid-1112",
"password": "M3PZAXgW5V27"
}
contact = ContactPojo(phone_number="755667750", passport_number="5123456789", last_name="PAN", first_name="Lei",
mail="panleicim@gmail.com", ccid="", position=0)
CommandorPage().start_page(None, contact)
asyncio.gather(main(), wait_for_otp())
time.sleep(1000)