簡體   English   中英

為什么我不能在我的 Python 代碼中使用 Playwright 與此元素交互(填充、單擊等)?

[英]Why can't I interact (fill, click, etc) with this element using Playwright in my Python code?

我正在使用 Playwright 訪問網站並與之交互,它一直很完美,直到我發現自己在無法與任何按鈕或搜索欄交互以應用過濾器的頁面中。 我可以使用.locator('xpath')來查找元素,但是當我嘗試 .click( .click('xpath').fill('xpath')甚至.locator ('xpath').click()時,我收到以下錯誤:

Traceback (most recent call last):
  File "c:\Users\Usuario\Desktop\Python Files\join\necessidades\join.py", line 24, in <module>
    pagina.locator('//*[@id="jrhFrm:barFiltro:filtros:nomeDoCurso_hinput"]').click()
  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python39\lib\site-packages\playwright\sync_api\_generated.py", line 13670, in click
    self._sync(
  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python39\lib\site-packages\playwright\_impl\_sync_base.py", line 104, in _sync
    return task.result()
  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python39\lib\site-packages\playwright\_impl\_locator.py", line 146, in click
    return await self._frame.click(self._selector, strict=True, **params)
  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python39\lib\site-packages\playwright\_impl\_frame.py", line 489, in click
    await self._channel.send("click", locals_to_params(locals()))
  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python39\lib\site-packages\playwright\_impl\_connection.py", line 44, in send
    return await self._connection.wrap_api_call(
  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python39\lib\site-packages\playwright\_impl\_connection.py", line 419, in wrap_api_call
    return await cb()
  File "C:\Users\Usuario\AppData\Local\Programs\Python\Python39\lib\site-packages\playwright\_impl\_connection.py", line 79, in inner_send
    result = next(iter(done)).result()
playwright._impl._api_types.TimeoutError: Timeout 30000ms exceeded.
=========================== logs ===========================
waiting for locator("xpath=//*[@id=\"jrhFrm:barFiltro:filtros:nomeDoCurso_hinput\"]")

這是對頁面的檢查~也許~有助於理解上下文。 我不知道為什么搜索欄在標簽內。

頁面檢查圖像

到目前為止我的代碼示例,以及 Codegen“建議”

from playwright.sync_api import sync_playwright
from time import sleep

with sync_playwright() as p:
    navegador = p.chromium.launch(headless=False)
    pagina = navegador.new_page()
    pagina.goto("page_url")
    
    pagina.fill('full_xpath from Username input','USERNAME')
    pagina.fill('full_xpath from Password input', 'Password')
    pagina.click('full_xpath from Enter button')
  
    try:
        pagina.click('full_xpath from a boring pop-up that sometimes shows up')
    except:
        pass
    
    sleep(10) #waiting the page to fully load

    pagina.click('full_xpath from the title of a Menu Item called Trainings')
    pagina.click('full_xpath from an Item called Course List that appeared from the Menu List')

    # HERE'S WHERE I'M HAVING PROBLEM
    sleep(5) #waiting the page to fully load
    pagina.locator('full_xpath from the search bar that I want to fill').fill('text I need to insert to search the Training')

    # THE BELOW CODE WAS GENERATED BY codegen
    pagina.frame_locator("#embedJoinRhJsf").locator("[id=\"jrhFrm\\:barFiltro\\:filtros\\:cursoPesquisa\"]").fill("TEXT") #raise an exception that I posted above in the comments

您是否嘗試過使用通用 CSS 選擇器? 不確定為什么你的 Xpath 選擇器不起作用,並假設你使用 xpaths 有一個特定的原因,但默認情況下我通常使用通用 CSS 選擇器而不是 xpaths 如果可以的話。 右鍵單擊該元素並復制 > 選擇器。 從你的照片來看,它看起來像是“#jrhFRM:barFiltro:filtros:cursoPesquisa”因此

pagina.locator("#jrhFRM:barFiltro:filtros:cursoPesquisa").click()

希望這有效。 另外,檢查您是否復制了正確的 xpath,作為錯誤消息中的 id ...

"jrhFrm:barFiltro:filtros:nomeDoCurso_hinput"

...看起來與您屏幕截圖中的 ID 不匹配...

"#jrhFRM:barFiltro:filtros:cursoPesquisa"

讓我們知道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM