Playwright 命令随机不起作用,导致超时错误

问题描述 投票:0回答:1

我正在使用 python playwright 制作一个机器人,它可以工作,但在随机时间,随机命令将被跳过或不起作用,导致下一个命令超时。


from multiprocessing.connection import Client
from playwright.sync_api import sync_playwright
from bs4 import BeautifulSoup
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from bson import ObjectId
Import Time
price1 = 0
price12 = 0
price_state1 = 0
price_state2 = 0
num = 0
While True:
    def run_playwright_test():
        with sync_playwright() as p:
# Launch the browser (headless=False to see the browser)
            browser = p.firefox.launch(headless = False)
            Context = browser.new_context()
           Context.set_default_timeout (0)
            Page = Context.new_page()
            Global price1
            Global price2
            Global price_state1
            Global price_state2
           page.goto('https://rivalregions.com/#overview')
            page.fill('input[name="mail"]', 'password)
            page.fill('input[name="p"]', 'password)
            page.click('input[name="s"]')
            # oil market
           page.locator('div[action="storage"].item_menu.storage_menu.ajax_action.header_menu_item.tc').click();
            page.click('div.tip.float_left.white.imp.storage_item.pointer.hov.ib_border');
            # oil
            page.wait_for_selector('div.storage_price')
            html_content = Page.Content()
            soup = BeautifulSoup(html_content, 'html.parser')
            price_span1 = soup.find('div', class_='storage_price').find('span', class_='dot')
            if price_span1:
                price_1 = price_span1.get_text(strip = True)
                price_1 = price_1.replace('$', '').replace(' ', '')  # Remove '$' and spaces
                price_1 = price_1.replace('.', '')  # Remove '$' and spaces
                price_1 = float(price_1)  # Convert to a numeric value (float)
                print(f"Extracted price: {price_1}")
            Else:
                Print ("Price not found")
            # ore market
           page.locator('div[action="storage"].item_menu.storage_menu.ajax_action.header_menu_item.tc').click();
            page.click('div.storage_number span[url="4"]')
            # ore
           page.wait_for_selector('div.storage_price')
            Time.sleep (10)
            html_content = Page.Content()
            soup = BeautifulSoup(html_content, 'html.parser')
            price_span2 = soup.find('div', class_='storage_price').find('span', class_='dot')
            if price_span2:
                price_2 = price_span2.get_text(strip = True)
                price_2 = price_2.replace('$', '').replace(' ', '')  # Remove '$' and spaces
                price_2 = price_2.replace('.', '')  # Remove '$' and spaces
                price_2 = float(price_2)
                print(f"Extracted price: {price_2}")
            Else:
                Print ("Price not found")

我正在使用树莓派 pi5 和 python 在 venv 中闲置

上面的代码只是其中的一小部分,它运行更多,似乎失败或跳过最多的是:

之前的行

page.wait_for_selector('div.storage_price')

请注意,我将超时更改为 0,这会停止错误,但仍然会暂停并且不会继续。

如果有人能告诉我为什么会发生这种情况以及如何解决它,那就太好了。

python timeout playwright python-idle
1个回答
0
投票

time.sleep(x)
导致一些问题
请参阅:https://playwright.dev/python/docs/library#timesleep-leads-to-outdated-state
更好用
page.wait_for_timeout(x * 1000)

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.