[Python] Can't find element by XPATH playwright

Discussão em 'Python' iniciado por Stack, Julho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get all search results (URLs) from https://docs.vgd.ru/search/?v=1 I am using xpath //a[@class='resultsMain'] the xpath is valid [​IMG]

    My code:

    import asyncio
    import time
    from playwright.async_api import async_playwright


    class VgdParser:
    def __init__(self, headless: bool = False):
    self.headless = headless
    self.browser = None
    self.playwright = None
    self.page = None


    async def start_browser(self):
    """Start the browser and create a new page with stealth mode"""
    self.playwright = await async_playwright().start()
    self.browser = await self.playwright.chromium.launch(
    headless=self.headless,
    )
    self.page = await self.browser.new_page()
    await self.page.goto("https://docs.vgd.ru/search/?v=1")

    async def search_by_name(self, name: str):
    """Enter name in https://docs.vgd.ru/search/?v=1 and collect results"""
    # Wait for the iframe to appear
    # Get the iframe (case-insensitive id)
    frame = self.page.frame(name="iFrame1")

    if frame is None:
    raise Exception("iframe with id 'iFrame1' not found")
    # Wait for the input inside the iframe
    input_locator = frame.locator('//input[@placeholder="Введите запрос"]')
    await input_locator.fill(name)
    await input_locator.press('Enter')

    frame = self.page.frame(name="iFrame1")
    if frame is None:
    raise Exception("iframe with id 'iFrame1' not found")
    results_raw = frame.locator("//a[@class='resultsMain']")
    count = await results_raw.count()
    print("XXX_ ", count) # PRINTS 0
    for i in range(count):
    cur_result = results_raw.nth(i)
    text = await cur_result.inner_text()
    print("Result:", text)

    async def main():
    parser = VgdParser()
    await parser.start_browser()
    await parser.search_by_name("Алексей Ермаков")

    if __name__ == "__main__":
    asyncio.run(main())


    Problem -- in function search_by_name line print("XXX_ ", count) prints 0 (didn't find element)

    Continue reading...

Compartilhe esta Página