Пример #1
0
def test_sync_playwright_multiple_times():
    with sync_playwright() as pw1:
        assert pw1.chromium
        with pytest.raises(Error) as exc:
            with sync_playwright() as pw2:
                assert pw1.chromium == pw2.chromium
        assert "Can only run one Playwright at a time." in exc.value.message
Пример #2
0
 def run(self):
     with sync_playwright() as playwright:
         browser = getattr(playwright, browser_name).launch()
         # This should not throw ^^.
         browser.new_page()
         browser.close()
         result.append("Success")
Пример #3
0
def run_with_playwright(rh, sheet):
    print("Running RPA Challenge with Playwright")
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        page = browser.newPage()
        page.goto("http://www.rpachallenge.com")
        page.click('text="Start"')
        for row in sheet:
            myeval(
                page, '//input[@ng-reflect-name="labelFirstName"]', row["First Name"]
            )
            myeval(page, '//input[@ng-reflect-name="labelLastName"]', row["Last Name"])
            myeval(
                page,
                '//input[@ng-reflect-name="labelCompanyName"]',
                row["Company Name"],
            )
            myeval(
                page, '//input[@ng-reflect-name="labelRole"]', row["Role in Company"]
            )
            myeval(page, '//input[@ng-reflect-name="labelAddress"]', row["Address"])
            myeval(page, '//input[@ng-reflect-name="labelEmail"]', row["Email"])
            myeval(
                page, '//input[@ng-reflect-name="labelPhone"]', str(row["Phone Number"])
            )
            page.evaluate("""document.querySelector('input[type="submit"]').click();""")
        result = page.evaluate(
            """() => {return document.querySelector('.message2').textContent;}"""
        )
        rh.check_for_new_record(page, result)
        browser.close()
def get_wicksly_info(tracking_number):
    secrets = Secrets()
    USER_NAME = secrets.get_secret("credentials")["PLATFORM_USERNAME"]
    PASSWORD = secrets.get_secret("credentials")["PLATFORM_PASS"]
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        wicksly_context = browser.newContext()
        w_page = wicksly_context.newPage()
        w_page.goto(os.environ.get('PLATFORM_URL'))
        w_page.fill('#id_username', USER_NAME)
        w_page.fill('#id_password', PASSWORD)
        with w_page.expect_navigation():
            w_page.click('input[type=submit]')
        w_page.goto(os.environ.get('PLATFORM_URL'))
        w_page.fill('#searchbar', tracking_number)

        with w_page.expect_navigation():
            w_page.click('text=Search')

        w_page.querySelectorAll('table#result_list tbody tr a')[0].click()
        street = w_page.getAttribute('#id_street1', 'value')
        city = w_page.getAttribute('#id_city', 'value')
        state = w_page.getAttribute('#id_state', 'value')
        postal_code = w_page.getAttribute('#id_postal_code', 'value')

        return {
            'street': street,
            'city': city,
            'state': state,
            'postal_code': postal_code
        }
Пример #5
0
def process_tang(tang_values):
    if not tang_values:
        return
    with sync_playwright() as playwright:
        browser_type = playwright.chromium
        executablePath = r'ms-playwright\chromium-833159\chrome-win\chrome.exe'
        browser = browser_type.launch(headless=False,
                                      executablePath=executablePath)
        context = browser.newContext()

        # Open new page
        page = context.newPage()
        page.setViewportSize(1920, 1040)

        # Go to http://tang.csair.com/WebFace/Tang.WebFace.Cargo/AgentAwbBrower.aspx?menuID=1
        page.goto(
            "http://tang.csair.com/WebFace/Tang.WebFace.Cargo/AgentAwbBrower.aspx?menuID=1"
        )

        for waybills_number in tang_values:
            prefix = waybills_number.split('-')[0]
            suffix = waybills_number.split('-')[-1]

            # Click input[name="ctl00$ContentPlaceHolder1$txtPrefix"]
            page.click("input[name=\"ctl00$ContentPlaceHolder1$txtPrefix\"]")
            # Fill input[name="ctl00$ContentPlaceHolder1$txtPrefix"]
            page.press("input[name=\"ctl00$ContentPlaceHolder1$txtPrefix\"]",
                       'Control+A')
            page.press("input[name=\"ctl00$ContentPlaceHolder1$txtPrefix\"]",
                       'Backspace')
            page.fill("input[name=\"ctl00$ContentPlaceHolder1$txtPrefix\"]",
                      prefix)

            # Click input[name="ctl00$ContentPlaceHolder1$txtNo"]
            page.click("input[name=\"ctl00$ContentPlaceHolder1$txtNo\"]")

            # Fill input[name="ctl00$ContentPlaceHolder1$txtNo"]
            page.press("input[name=\"ctl00$ContentPlaceHolder1$txtNo\"]",
                       'Control+A')
            page.press("input[name=\"ctl00$ContentPlaceHolder1$txtNo\"]",
                       'Backspace')
            page.fill("input[name=\"ctl00$ContentPlaceHolder1$txtNo\"]",
                      suffix)

            # Press Enter
            page.click("//input[@id='btnSearch']")
            # assert page.url == "http://tang.csair.com/WebFace/Tang.WebFace.Cargo/AgentAwbBrower.aspx?menuID=1"

            page.waitForLoadState(state="domcontentloaded")
            time.sleep(10)
            page.screenshot(path=f'{hx_path}/%s.png' % waybills_number)

        # Close page
        page.close()

        # ---------------------
        context.close()
        browser.close()
Пример #6
0
def page():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch()
        context = browser.newContext()
        page = context.newPage()
        yield page
        page.close()
        context.close()
        browser.close()
Пример #7
0
def get_playwright():
    global playwright
    if playwright is None:
        try:
            playwright = sync_playwright().start()
        except Exception as e:
            raise e

    return playwright
def page():
    with sync_playwright() as play:
        if os.getenv('DOCKER_RUN') or os.getenv('GITHUB_RUN'):
            browser = play.chromium.launch(headless=True,
                                           args=['--no-sandbox'])
        else:
            browser = play.chromium.launch(headless=False)
        page = browser.newPage()
        global PAGE
        PAGE = page
        yield page
        browser.close()
Пример #9
0
class play_wright(unittest.TestCase):
    playwright = sync_playwright().start()
    browser = playwright.chromium.launch(headless=False)
    page = browser.newContext().newPage()

    def test_a(self):
        self.page.goto("https://www.baidu.com")
        self.page.type("input[id=kw]", "Playwright GitHub")
        self.page.waitForTimeout(1000)
        self.page.click("input[id=su]")
        self.page.waitForTimeout(3000)
        text = self.page.querySelector('a[name="tj_login"]').textContent()
        print(text)
        self.page.waitForTimeout(3000)
        self.page.goBack()
        page2 = self.browser.newContext().newPage()
        page2.goto('https://google.com')
        self.page.bringToFront()
        self.page.waitForTimeout(3000)

    def test_c(self):
        self.page.setViewportSize(width=1920, height=1080)
        self.page.fill("input[id=kw]", '')
        self.page.type("input[id=kw]", "ps5")
        self.page.waitForTimeout(1000)
        self.page.click("input[id=su]")
        self.page.waitForTimeout(3000)
        self.page.keyboard.down(key="ArrowDown")
        if self.page.waitForSelector("//a[contains(text(),'下一页 >')]", 5000,
                                     'visible'):
            self.page.waitForTimeout(3000)
            self.page.click("//a[contains(text(),'下一页 >')]")
        else:
            self.page.keyboard.down(key="ArrowDown")
        # self.page.querySelector('a[name="tj_login"]').waitForElementState('visible',5000)
        self.page.querySelector('a[name="tj_login"]').textContent()
        self.page.querySelector('a[name="tj_login"]')
        # self.page.waitForTimeout(3000)
        self.page.querySelector(
            "//a[contains(text(),'下一页 >')]").scrollIntoViewIfNeeded()
        # self.page.waitForTimeout(2000)
        self.page.querySelector(
            "//a[contains(text(),'下一页 >')]").scrollIntoViewIfNeeded()
        self.page.waitForTimeout(3000)
        self.page.context.on()
        # if self.page.waitForSelector('a[name="tj_login"]', 5000,'visible'):
        #     self.page.click('a[name="tj_login"]')
        #     self.page.waitForTimeout(3000)
        self.page.close()

    def test_d(self):
        self.page.setViewportSize(width=1920, height=1080)
Пример #10
0
def browser():
    pw_context = sync_playwright()
    pw = pw_context.__enter__()
    browser = getattr(pw, "chromium").connect(
        wsEndpoint='wss://chrome.headlesstesting.com?token=[YOUR-TOKEN]')
    browser._close = browser.close

    def _handle_close() -> None:
        browser._close()
        pw_context.__exit__(None, None, None)

    browser.close = _handle_close

    return browser
def browser():
    with sync_playwright() as p:
        try:
            video_location = f"{os.path.dirname(os.path.abspath(__file__))}"
            new_browser = p.chromium.launch(headless=False)
            page = new_browser.newPage(
                viewport={"width": 1680, "height": 1080},
                videosPath=f"{video_location}"
            )
            yield page
            new_browser.close()
            p.stop()
        except Exception as e:
            print(e)
            pytest.skip("Exception")
Пример #12
0
    def launch(**kwargs: Dict[Any, Any]) -> Browser:
        headful_option = pytestconfig.getoption("--headful")
        launch_options = {**browser_type_launch_args, **kwargs}
        if headful_option:
            launch_options["headless"] = False
        pw = sync_playwright().start()
        browser = getattr(pw, browser_name).launch(**launch_options)
        browser._close = browser.close

        def _handle_close() -> None:
            browser._close()
            pw.stop()

        browser.close = _handle_close
        return browser
Пример #13
0
def main():
    """Set up playwright and print ballot details."""
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch()
        context = browser.newContext()
        page = context.newPage()

        details = get_details(page, INPUTS)

        page.close()
        context.close()
        browser.close()

    for field, value in details._asdict().items():
        print(f"{field:10} {value}")
Пример #14
0
    def launch(**kwargs: Dict[Any, Any]) -> Browser:
        headful_option = pytestconfig.getoption("--headful")
        launch_options = {**launch_arguments, **kwargs}
        if headful_option:
            launch_options["headless"] = False
        pw_context = sync_playwright()
        pw = pw_context.__enter__()
        browser = getattr(pw, browser_name).launch(**launch_options)
        browser._close = browser.close

        def _handle_close() -> None:
            browser._close()
            pw_context.__exit__(None, None, None)

        browser.close = _handle_close
        return browser
Пример #15
0
def get_recent_call_history(phone_number, password, national_id):
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.newPage()
        # login
        page.goto('https://myais.ais.co.th/login')
        page.type("input[name='a mobile input']",phone_number)
        page.click("button[type=submit]")
        page.click("button[id=btnLoginFormPassword]")
        page.type("input[id='txtLoginFormPassword']", password)
        page.click("button[id=btnLoginFormSubmitLogin2]")

        # access page
        page.waitForSelector("myais-sidebar-menu:nth-child(4) .menu-content")
        page.goto("https://myais.ais.co.th/balance/usage")

        #fill national id
        page.type("input[id='input-idcard']", national_id)

        # XHR **/invoice
        url = '**/invoices'
        with page.expect_response(url):
            page.click("button[type=submit]")
            response = page.waitForResponse(url)
            invoices = response.json()
            logger.debug(invoices)
        # page.on('request', print)


        # in case you want other bill hisory
        # .check-box .check , .check-box img
        # .check-box img = [bill1]
        # .billing-row-detail:nth-child(3) .check [bill2]
        # .billing-row-detail:nth-child(4) .check [bill3]

        # XHR call history(**/filter)
        url = "**/usages/billed/group/VOICE/chargedetail/filter"
        with page.expect_response(url):
            page.click("#btnApproveUsageDetailSeaching")
            response = page.waitForResponse(url)
            call_usage = response.json()
            logger.debug(call_usage)


        browser.close()

        return call_usage
Пример #16
0
def extract_leads(search_url):
    with sync_playwright() as p:
        generated_data = []
        browser = p.webkit.launch(headless=False)
        context = browser.newContext()
        page = context.newPage()
        login(page)
        page.goto(search_url)
        page.click('.search-results__pagination-next-button')
        page.waitForResponse('**people**')
        page.click('.search-results__pagination-previous-button')
        first_response = page.waitForResponse('**people**')
        json_response = first_response.json()
        generated_data = generated_data + process_json(json_response)
        while has_next_button(page):
            generated_data = crawl_page(page, generated_data)
        output_csv(generated_data)
Пример #17
0
def process_cargo(cargo_values):
    if not cargo_values:
        return
    with sync_playwright() as playwright:
        browser_type = playwright.chromium
        executablePath = r'ms-playwright\chromium-833159\chrome-win\chrome.exe'
        browser = browser_type.launch(headless=False,
                                      executablePath=executablePath)
        context = browser.newContext()

        # Open new page
        page = context.newPage()
        page.setViewportSize(1920, 1040)

        # Go to https://cargo.china-airlines.com/CCNetv2/content/manage/ShipmentTracking.aspx
        page.goto(
            "https://cargo.china-airlines.com/CCNetv2/content/manage/ShipmentTracking.aspx"
        )

        for waybills_number in cargo_values:
            suffix = waybills_number.split('-')[-1]
            # Click input[name="ctl00$ContentPlaceHolder1$txtAwbNum"]
            page.click("input[name=\"ctl00$ContentPlaceHolder1$txtAwbNum\"]")

            # Fill input[name="ctl00$ContentPlaceHolder1$txtAwbNum"]
            page.press("input[name=\"ctl00$ContentPlaceHolder1$txtAwbNum\"]",
                       'Control+A')
            page.press("input[name=\"ctl00$ContentPlaceHolder1$txtAwbNum\"]",
                       'Backspace')
            page.fill("input[name=\"ctl00$ContentPlaceHolder1$txtAwbNum\"]",
                      suffix)

            # Click input[name="ctl00$ContentPlaceHolder1$btnSearch"]
            page.click("input[name=\"ctl00$ContentPlaceHolder1$btnSearch\"]")
            # assert page.url == "https://cargo.china-airlines.com/CCNetv2/content/manage/ShipmentTracking.aspx"

            page.waitForLoadState(state="networkidle")
            page.screenshot(path=f'{hx_path}/%s.png' % waybills_number)

        # Close page
        page.close()

        # ---------------------
        context.close()
        browser.close()
Пример #18
0
def get_talks_schedule():
    charlas = []
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.newPage()

        # Visit Schedule Page
        page.goto('https://eventos.python.org.ar/events/pyconar2020/schedule')

        # Get all divs with the schedule of the day
        days = page.querySelectorAll('css=div .well')
        for day in days:
            # Get the day header text
            dia = day.querySelector('h2').innerText().strip()
            print(dia)

            dia = dia.split('- ')[1]  # Día 1 - 16/11/2020 --> 16/11/2020
            # Get all talks from that day.
            charlas_divs = day.querySelectorAll('css=div .fc-content')
            for charla in charlas_divs:
                """
                Expected html
                    <div class="fc-time" data-start="19:00" data-full="7:00 PM - 8:00 PM">
                        <span>19:00 - 20:00</span>
                    </div>
                    <div class="fc-title">¿Por que no se hacen mas tests?</div>
                """
                # For each talk, get its schedule and its title
                horario = charla.querySelector('.fc-time').innerText()
                start, end = horario.split(' - ')
                start_date, end_date = parse_dates(dia, start, end)

                titulo = charla.querySelector('.fc-title').innerText()
                print('Charla ', titulo)
                charlas.append({
                    'day': str(dia),
                    'titulo': titulo,
                    'start': start_date.isoformat(),
                    'end': end_date.isoformat(),
                })

        browser.close()

    return charlas
Пример #19
0
 def cookie_loggin(self):
     '''自动登录操作'''
     self.judge_cookie()  # 首先判断cookie是否已获取,是否过期
     print("自动登录开始...")
     # 加启动配置
     playwright = sync_playwright().start()
     #无头浏览器模式
     browser = playwright.chromium.launch()
     #打开浏览器模式
     #browser = playwright.chromium.launch(headless=False,slowMo=200)
     context = browser.newContext()
     #设置防爬的参数
     context.addInitScript(source="""
                     const newProto = navigator.__proto__;
                     delete newProto.webdriver;
                     navigator.__proto__ = newProto;
         """)
     #设置超时时间为30s
     context.setDefaultTimeout(30000)
     page = context.newPage()
     #打开权力运行系统登陆网址
     page.goto(self.log_url, waitUntil="load")
     context.clear_cookies()  # 清除旧cookies
     # with可以上下文管理上文进行设置部署,下文进行处理,然后把处理的结果赋值给变量(cookie)
     with open(os.path.join(self.cookie_path, self.cookie_name),
               'r') as cookief:
         #使用json读取cookies 注意读取的是文件 所以用load而不是loads
         cookieslist = json.load(cookief)
         # 方法1删除该字段
         cookies_dict = dict()
         for cookie in cookieslist:
             #该字段有问题所以删除就可以,浏览器打开后记得刷新页面 有的网页注入cookie后仍需要刷新一下
             if 'expiry' in cookie:
                 del cookie['expiry']
             cookies_dict[cookie['name']] = cookie['value']
     context.add_cookies(cookies_dict)
     page.goto(self.home_url, waitUntil='load')
     #等待10s查看网页是否cookies登录
     time.sleep(10)
     page.close()
     context.close()
     browser.close()
     print("浏览器退出")
def playwright_start(login_url):
    playwright = sync_playwright().start()
    #无头浏览器模式
    #browser = playwright.chromium.launch()
    #打开浏览器模式
    browser = playwright.chromium.launch(headless=False, slowMo=200)
    context = browser.newContext()
    #设置防爬的参数
    context.addInitScript(source="""
                    const newProto = navigator.__proto__;
                    delete newProto.webdriver;
                    navigator.__proto__ = newProto;
        """)
    #设置超时时间为30s
    context.setDefaultTimeout(30000)
    page = context.newPage()
    # page.on("request", lambda request: print("Request %s" % request.url))
    # page.on("requestfinished", lambda request: print("Request finished %s" % request.url))
    # page.on("response",lambda response: print("Response %s"% (response.text())))
    page.goto(login_url, waitUntil="networkidle")
    with open(r'.\date\1.html', 'w', encoding='utf-8') as f:
        f.write(page.content())
    # deptname_list=page.querySelectorAll('//div[@class="pub-people"]/ul[1]/div/li[@class="depItem"]/a')
    # for deptname in deptname_list:
    #     dept_name=deptname.getAttribute("title")
    #     dept_url=deptname.getAttribute("href")
    html = etree.HTML(page.content())
    deptname_list = html.xpath(
        '//div[@class="pub-people"]/ul[1]/div/li[@class="depItem"]')
    for deptname in deptname_list:
        dept_name = deptname.xpath('.//a/@title')[0]
        dept_url = deptname.xpath('.//a/@href')[0]
        content = {
            "单位名称": dept_name,
            "网站链接": dept_url,
        }
        print(content)
        contents.append(content)
    page.close()
    context.close()
    browser.close()
Пример #21
0
 def get_cookie(self):
     '''登录获取cookie'''
     playwright = sync_playwright().start()
     #无头浏览器模式
     browser = playwright.chromium.launch()
     #打开浏览器模式
     #browser = playwright.chromium.launch(headless=False,slowMo=200)
     context = browser.newContext()
     #设置防爬的参数
     context.addInitScript(source="""
                     const newProto = navigator.__proto__;
                     delete newProto.webdriver;
                     navigator.__proto__ = newProto;
         """)
     #设置超时时间为30s
     context.setDefaultTimeout(30000)
     page = context.newPage()
     #打开权力运行系统登陆网址
     page.goto(self.log_url, waitUntil="load")
     page.fill("input[name=\"userNameHeader\"]", USENAME)
     page.fill("input[name=\"password\"]", PASSWORD)
     page.check("input[id=\"recordAccount\"]")
     with page.expect_navigation():
         page.click('//a[@class="btn-login"]')
         print("登陆成功")
     page.waitForSelector(
         r'//div[@class="lw-navContent"]/ul/li/a[contains(text(), "权力事项管理")]'
     )
     page.click(
         r'//div[@class="lw-navContent"]/ul/li/a[contains(text(), "权力事项管理")]'
     )
     print("进入权力事项管理")
     with open(os.path.join(self.cookie_path, self.cookie_name),
               'w') as cookief:  # 创建文本覆盖保存cookie
         # 将cookies保存为json格式
         cookief.write(json.dumps(context.cookies()))
     print("cookies保存完成")
     page.close()
     context.close()
     browser.close()
Пример #22
0
from playwright import sync_playwright

with sync_playwright() as p:
    for browser_type in [p.chromium, p.firefox, p.webkit]:
        browser = browser_type.launch(headless=False)
        page = browser.newPage()
        page.goto('http://whatsmyuseragent.org/')
        page.screenshot(path=f'screenshots/example-{browser_type.name}.png')
        browser.close()
Пример #23
0
def run(excel_file):
    with sync_playwright() as playwright:
        browser_type = playwright.chromium
        executablePath = r'ms-playwright\chromium-833159\chrome-win\chrome.exe'
        browser = browser_type.launch(headless=False,
                                      executablePath=executablePath)
        context = browser.newContext()

        # Open new page
        page = context.newPage()
        page.setViewportSize(1920, 1040)
        logger.info(' - 开始登陆网站')
        # Go to http://uniner.synology.me:88/LKT/index.php
        page.goto("http://uniner.synology.me:88/LKT/index.php")

        # Go to http://uniner.synology.me:88/LKT/index.php?module=Login
        page.goto("http://uniner.synology.me:88/LKT/index.php?module=Login")

        # Click input[name="login"]
        page.click("input[name=\"login\"]")

        # Fill input[name="login"]
        page.fill("input[name=\"login\"]", "laiketui")

        # Click input[name="pwd"]
        page.click("input[name=\"pwd\"]")

        # Fill input[name="pwd"]
        page.fill("input[name=\"pwd\"]", "laiketui")

        # Press Enter
        # with page.expect_navigation(url="http://uniner.synology.me:88/LKT/index.php?module=AdminLogin"):
        with page.expect_navigation():
            page.press("input[name=\"pwd\"]", "Enter")

        logger.info(' - 网站登录成功')
        page.waitForLoadState('networkidle')

        # Click //dt[normalize-space(.)='商品管理']
        page.click("//dt[normalize-space(.)='商品管理']")

        # Click text="商品列表"
        page.click("text=\"商品列表\"")

        frame_product = page.frames[2]

        # result = input('是否删除之前商品信息 (y/n) :')
        logger.info(' - 开始自动删除历史数据')
        while True:
            # if result != 'y':
            #     break

            if frame_product.textContent(
                    '//div[@class="mt-20"]/div/table/tbody').strip():
                frame_product.click("//label")
                frame_product.click("text=/.*删除.*/")
                frame_product.click("text=\"确认\"")
                frame_product.click("text=\"确认\"")
            else:
                logger.info(' - 历史商品信息全部删除成功')
                time.sleep(2)
                break
        logger.info(' - 10s后开始自动录入数据...')
        time.sleep(10)

        logger.info(' - 开始读取并处理Excel表格数据')
        df = pd.read_excel(excel_file)

        logger.info(' - 开始自动上传商品信息')
        for i in df.index:
            sku = df.loc[i, 'sku']
            title = df.loc[i, '标题']
            s_price = int(df.loc[i, '售价'])
            m_price = int(df.loc[i, '市场价'])
            img_path = df.loc[i, '图片路径']

            # Click text=/.*发布商品.*/
            frame_product.click("text=/.*发布商品.*/")

            frame_product.waitForLoadState('networkidle')

            # Fill input[name="product_title"]
            frame_product.fill("input[name=\"product_title\"]", title)  # 商品标题

            # Fill input[name="subtitle"]
            frame_product.fill("input[name=\"subtitle\"]", title)  # 副标题

            frame_product.selectOption('//*[@id="product_classId"]',
                                       '-7-28-')  # 商品类别

            frame_product.selectOption('//*[@id="brand_classId"]', '9')  # 商品品牌

            frame_product.click('//*[@id="image"]')

            frame_product.click("text=/.*本地上传.*/")

            # Upload 003dd00372c3f98156512c62afe559cc.jpg
            frame_product.setInputFiles("input[name=\"imgFile\"]",
                                        img_path)  # 商品主图

            frame_product.click("text=/.*确定.*/")

            # Upload 微信图片_20201208093221.jpg
            frame_product.setInputFiles("input[name=\"imgurls[]\"]",
                                        img_path)  # 商品展示图

            # Fill input[name="initial[cbj]"]
            frame_product.fill("input[name=\"initial[cbj]\"]",
                               f'{int(s_price * 0.5)}')  # 成本价

            # Fill input[name="initial[yj]"]
            frame_product.fill("input[name=\"initial[yj]\"]",
                               f'{m_price}')  # 原价

            # Fill input[name="initial[sj]"]
            frame_product.fill("input[name=\"initial[sj]\"]",
                               f'{s_price}')  # 售价

            frame_product.selectOption('//*[@id="unit"]', '双')  # 单位

            # Fill input[name="initial[kucun]"]
            frame_product.fill("input[name=\"initial[kucun]\"]",
                               f'{random.randint(1000, 10000)}')  # 库存

            # Fill input[placeholder="请输入属性名称"]
            frame_product.fill("input[placeholder=\"请输入属性名称\"]",
                               f'{sku}')  # sku

            frame_product.click(
                f'//label[@for="sex-{random.randint(1, 4)}"]')  # 类型

            frame_product.hover('//*[@id="form1"]//input[@name="Submit"]')

            frame_product.fill('//input[@id="volumeId"]',
                               f'{random.randint(500, 5000)}')  # 拟定销量

            frame_product.selectOption('//select[@id="freightId"]',
                                       f'{random.randint(1, 2)}')  # 运费设置

            frame_ueditor_0 = page.frames[4]
            frame_ueditor_0.addScriptTag(
                content='document.body.innerHTML="{}"'.format('无'))

            page.once("dialog", lambda dialog: click_alert(dialog))
            frame_product.click(
                '//*[@id="form1"]//input[@name="Submit"]')  # 提交
            msg = '第%s款产品发布成功!' % (i + 1)
            logger.info(' - %s' % msg)

        else:
            page.click("text=\"商品列表\"")
            time.sleep(2)
            logger.info(' - 商品信息全部上传完成')
            time.sleep(2)
            logger.info(' - 自动录入完成,60后自动关闭程序')

        time.sleep(56)

        logger.info(' - 程序结束')

        # Close page
        page.close()

        # ---------------------
        context.close()
        browser.close()
Пример #24
0
def main():
    with sync_playwright() as playwright:
        run(playwright)
    get_repositories(url)
Пример #25
0
def run(tracking_number, shipment_info):
    ORIGIN_COMPANY = os.environ.get('ORIGIN_COMPANY')
    ORIGIN_STREET = os.environ.get('ORIGIN_STREET')
    ORIGIN_ZIP = os.environ.get('ORIGIN_ZIP')
    ORIGIN_CITY = os.environ.get('ORIGIN_CITY')
    ORIGIN_STATE = os.environ.get('ORIGIN_STATE')
    ORIGIN_PHONE = os.environ.get('ORIGIN_PHONE')

    CONTACT_EMAIL = os.environ.get('CONTACT_EMAIL')
    CONTACT_FIRST = os.environ.get('CONTACT_FIRST')
    CONTACT_LAST = os.environ.get('CONTACT_LAST')

    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.newContext()
        page = context.newPage()
        page.goto('https://www.usps.com/help/missing-mail.htm')
        page.click("text=\"Start Your Form\"")
        page.fill("input[type=\"text\"]", tracking_number)
        with page.expect_navigation():
            page.click("text=\"Track\"")

        page.click("text=\"Start\"")
        page.click("a[role=\"button\"]")
        page.click(
            "//a[normalize-space(.)='Package' and normalize-space(@title)='Package' and normalize-space(@role)='menuitemradio']"
        )
        page.click(
            "//a[normalize-space(.)='--None--' and normalize-space(@role)='button']"
        )

        # Click //a[normalize-space(.)='First-Class Mail®' and normalize-space(@title)='First-Class Mail®' and normalize-space(@role)='menuitemradio']
        page.click(
            "//a[normalize-space(.)='First-Class Mail®' and normalize-space(@title)='First-Class Mail®' and normalize-space(@role)='menuitemradio']"
        )

        # Click //a[normalize-space(.)='--None--' and normalize-space(@role)='button']
        page.click(
            "//a[normalize-space(.)='--None--' and normalize-space(@role)='button']"
        )

        # Click //a[normalize-space(.)='Business' and normalize-space(@title)='Business' and normalize-space(@role)='menuitemradio']
        page.click(
            "//a[normalize-space(.)='Business' and normalize-space(@title)='Business' and normalize-space(@role)='menuitemradio']"
        )

        # Click //a[normalize-space(.)='--None--' and normalize-space(@role)='button']
        page.click(
            "//a[normalize-space(.)='--None--' and normalize-space(@role)='button']"
        )

        # Click (//a[normalize-space(.)='Residence' and normalize-space(@title)='Residence' and normalize-space(@role)='menuitemradio'])[2]
        page.click(
            "(//a[normalize-space(.)='Residence' and normalize-space(@title)='Residence' and normalize-space(@role)='menuitemradio'])[2]"
        )

        # Check //div[normalize-space(.)='None']/input[normalize-space(@type)='checkbox']
        page.check(
            "//div[normalize-space(.)='None']/input[normalize-space(@type)='checkbox']"
        )

        # Click //div[normalize-space(.)='Unknown' and normalize-space(@role)='option']
        page.click(
            "//div[normalize-space(.)='Unknown' and normalize-space(@role)='option']"
        )

        # Click //lightning-primitive-icon
        page.click("//lightning-primitive-icon")

        # Click input[type="text"]
        page.click("input[type=\"text\"]")

        # Fill input[type="text"]
        page.fill("input[type=\"text\"]", "15")

        # Click text="Next"
        page.click("text=\"Next\"")

        page.waitForSelector(
            "//label[span[text()='Street Address']]/following-sibling::input")
        page.fill(
            "//label[span[text()='Street Address']]/following-sibling::input",
            ORIGIN_STREET)

        # Click //div[normalize-space(.)='City']/input[normalize-space(@type)='text']
        page.click(
            "//div[normalize-space(.)='City']/input[normalize-space(@type)='text']"
        )

        # Fill //div[normalize-space(.)='City']/input[normalize-space(@type)='text']
        page.fill(
            "//div[normalize-space(.)='City']/input[normalize-space(@type)='text']",
            ORIGIN_CITY)

        page.click(
            "//div[normalize-space(@role)='list']/div[4]/div[1][normalize-space(@role)='listitem']/div/div/div/div/div[1]/div/div/a[normalize-space(.)='--None--' and normalize-space(@role)='button']"
        )

        page.click(
            f"//a[normalize-space(.)='{ORIGIN_STATE}' and normalize-space(@title)='{ORIGIN_STATE}' and normalize-space(@role)='menuitemradio']"
        )

        page.click(
            "//div[normalize-space(.)='ZIP Code™']/input[normalize-space(@type)='text']"
        )

        # Click //div[normalize-space(.)='ZIP Code™']/input[normalize-space(@type)='text']
        page.click(
            "//div[normalize-space(.)='ZIP Code™']/input[normalize-space(@type)='text']"
        )

        # Fill //div[normalize-space(.)='ZIP Code™']/input[normalize-space(@type)='text']
        page.fill(
            "//div[normalize-space(.)='ZIP Code™']/input[normalize-space(@type)='text']",
            ORIGIN_ZIP)

        page.waitForSelector(
            "//div[normalize-space(.)='Street Address*']/input[normalize-space(@type)='text']"
        )
        # Click //div[normalize-space(.)='Street Address*']/input[normalize-space(@type)='text']
        page.click(
            "//div[normalize-space(.)='Street Address*']/input[normalize-space(@type)='text']"
        )

        # Fill //div[normalize-space(.)='Street Address*']/input[normalize-space(@type)='text']
        page.fill(
            "//div[normalize-space(.)='Street Address*']/input[normalize-space(@type)='text']",
            shipment_info['street'])

        # Click (//div[normalize-space(@role)='listitem']/div/div/div[normalize-space(.)='Apt/Suite/Other']/input[normalize-space(@type)='text'])[2]
        page.click(
            "(//div[normalize-space(@role)='listitem']/div/div/div[normalize-space(.)='Apt/Suite/Other']/input[normalize-space(@type)='text'])[2]"
        )
        page.click(
            "//div[normalize-space(.)='City*']/input[normalize-space(@type)='text']"
        )
        page.fill(
            "//div[normalize-space(.)='City*']/input[normalize-space(@type)='text']",
            shipment_info['city'])
        page.click(
            "(//div[normalize-space(@role)='listitem']/div/div/div/div/div[1]/div/div/a[normalize-space(.)='--None--' and normalize-space(@role)='button'])[2]"
        )

        page.querySelectorAll(
            f"//a[starts-with(@title, '{shipment_info['state']}') and normalize-space(@role)='menuitemradio']"
        )[1].click()
        page.click(
            "//div[normalize-space(.)='ZIP Code™*']/input[normalize-space(@type)='text']"
        )
        page.fill(
            "//div[normalize-space(.)='ZIP Code™*']/input[normalize-space(@type)='text']",
            shipment_info['postal_code'])

        page.click("text=\"Next\"")
        page.waitForSelector(
            "//div[normalize-space(.)='First Name*']/input[normalize-space(@type)='text']"
        )
        page.fill(
            "//div[normalize-space(.)='First Name*']/input[normalize-space(@type)='text']",
            CONTACT_FIRST)
        page.fill(
            "//div[normalize-space(.)='Middle Name']/input[normalize-space(@type)='text']",
            "")
        page.press(
            "//div[normalize-space(.)='Middle Name']/input[normalize-space(@type)='text']",
            "Tab")
        page.fill(
            "//div[normalize-space(.)='Last Name*']/input[normalize-space(@type)='text']",
            CONTACT_LAST)
        page.press(
            "//div[normalize-space(.)='Last Name*']/input[normalize-space(@type)='text']",
            "Tab")
        page.fill(
            "(//div[normalize-space(@role)='listitem']/div/div/div[normalize-space(.)='Street Address*']/input[normalize-space(@type)='text'])[2]",
            ORIGIN_STREET)
        page.click(
            "(//div[normalize-space(@role)='listitem']/div/div/div[normalize-space(.)='City*']/input[normalize-space(@type)='text'])[2]"
        )
        page.fill(
            "(//div[normalize-space(@role)='listitem']/div/div/div[normalize-space(.)='City*']/input[normalize-space(@type)='text'])[2]",
            ORIGIN_CITY)
        page.click(
            "(//div[normalize-space(@role)='listitem']/div/div/div/div/div[1]/div/div/a[normalize-space(.)='--None--' and normalize-space(@role)='button'])[2]"
        )
        page.click(
            f"(//a[normalize-space(.)='{ORIGIN_STATE}' and normalize-space(@title)='{ORIGIN_STATE}' and normalize-space(@role)='menuitemradio'])[3]"
        )
        page.click(
            "(//div[normalize-space(@role)='listitem']/div/div/div[normalize-space(.)='ZIP Code™*']/input[normalize-space(@type)='text'])[2]"
        )
        page.fill(
            "(//div[normalize-space(@role)='listitem']/div/div/div[normalize-space(.)='ZIP Code™*']/input[normalize-space(@type)='text'])[2]",
            ORIGIN_ZIP)

        page.click(
            "//div[normalize-space(.)='Email*']/input[normalize-space(@type)='text']"
        )
        page.fill(
            "//div[normalize-space(.)='Email*']/input[normalize-space(@type)='text']",
            CONTACT_EMAIL)
        page.press(
            "//div[normalize-space(.)='Email*']/input[normalize-space(@type)='text']",
            "Tab")
        page.fill(
            "//div[normalize-space(.)='Retype Email*']/input[normalize-space(@type)='text']",
            CONTACT_EMAIL)
        page.press(
            "//div[normalize-space(.)='Retype Email*']/input[normalize-space(@type)='text']",
            "Tab")
        page.fill("input[type=\"tel\"]", ORIGIN_PHONE)
        page.click(
            "(//div[normalize-space(@role)='listitem']/div/div/div/div/div[1]/div/div/a[normalize-space(.)='--None--' and normalize-space(@role)='button'])[2]"
        )
        page.click(
            "(//a[normalize-space(.)='Business' and normalize-space(@title)='Business' and normalize-space(@role)='menuitemradio'])[3]"
        )
        page.click(
            "//div[normalize-space(.)='Company']/input[normalize-space(@type)='text']"
        )
        page.fill(
            "//div[normalize-space(.)='Company']/input[normalize-space(@type)='text']",
            ORIGIN_COMPANY)
        page.click("//div[normalize-space(.)='PreviousFinishCancel']")

        page.click("text=\"Finish\"")
        page.click("text=\"Submit\"")
        page.close()
        context.close()
        browser.close()
Пример #26
0
def visit(browser, method, url):
    playwright = sync_playwright().start()
    browsers = playwright.chromium.launch(headless=False)
    page = browsers.newPage()
    page.goto(url)
Пример #27
0
def playwright() -> Generator[Any, None, None]:
    pw = sync_playwright().start()
    yield pw
    pw.stop()
Пример #28
0
    page2.goto("https://webkit.org")
    print("Page2 tile: %s" % page2.title())
    print("Pages in context: %d" % len(context.pages))

    print("\nQuerying body...")
    body1 = page1.query_selector("body")
    assert body1
    print("Body text %s" % body1.text_content())

    print("Closing page1...")
    page1.close()
    print("Pages in context: %d" % len(context.pages))

    print("Navigating page2 to https://cnn.com...")
    page2.goto("https://cnn.com")
    print("Page2 main frame url: %s" % page2.main_frame.url)
    print("Page2 tile: %s" % page2.title())
    print("Frames in page2: %d" % len(page2.frames))
    print("Pages in context: %d" % len(context.pages))

    print("Closing context...")
    context.close()
    print("Contexts in browser: %d" % len(browser.contexts))
    print("Closing browser")
    browser.close()


if __name__ == "__main__":
    with playwright.sync_playwright() as p:
        main(p)
Пример #29
0
import logging
import random
import string
import requests

# Import Detection From Stealth
from .get_acrawler import get_acrawler
from playwright import sync_playwright

try:
    playwright = sync_playwright().start()
except Exception as e:
    raise e

async_support = False

logger = logging.getLogger(__name__)


def get_playwright():
    return playwright


class browser:
    def __init__(
        self,
        **kwargs,
    ):
        self.debug = kwargs.get("debug", False)
        self.proxy = kwargs.get("proxy", None)
        self.api_url = kwargs.get("api_url", None)
Пример #30
0
def playwright():
    with sync_playwright() as p:
        yield p