基于Playwright的Python机器人随机执行失败/命令跳过导致卡住问题求助
基于Playwright的Python机器人随机执行失败/命令跳过导致卡住问题求助
我在用Python Playwright开发一个机器人,它多数时候能正常运行,但会随机出现命令被跳过或不执行的情况,进而导致后续命令超时卡住。
问题详情
我在树莓派5的虚拟环境中使用Python IDLE运行该程序。目前观察到的核心现象是:随机有操作命令未被执行,尤其是page.wait_for_selector('div.storage_price')这行代码之前的交互步骤,即便我已经设置了Context.set_default_timeout(0)(禁用超时),程序依然会卡住无法继续推进。
代码片段
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")
请问有人能帮我分析这个问题的根源,并给出对应的解决办法吗?
备注:内容来源于stack exchange,提问作者Jackson Taylor
相关产品推荐
相关产品推荐

