[英]Why does my Python webcrawler throw an error that it can't interact with the selected element?
[英]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.