python {TimeoutError}TimeoutError('超过30000 ms.')Playwright

cnh2zyt3  于 2024-01-05  发布在  Python
关注(0)|答案(1)|浏览(187)

我在尝试点击定位器元素时遇到了一个问题。我使用这段代码page.locator('span[dir="auto"]:has-text("See more comments")').all()来获取与选择器匹配的所有定位器,并且工作正常,因为它检索8个元素。每个元素都显示一个弹出窗口,其中包含我需要的信息,但是使用for循环来遍历每个元素,并尝试在其上创建.click(),结果是{TimeoutError}TimeoutError('Timeout 30000ms exceeded.')。即使我尝试点击第一个元素而不检索所有元素,也会导致此错误。我不知道为什么会发生这种情况。希望你能帮助我。代码如下:

  1. from playwright.sync_api import sync_playwright
  2. from mypackages.service import CookiesFacebookService()
  3. from mypackages.utils import Options
  4. class PlaywrightScraper:
  5. def __init__(self):
  6. self.counter = 0
  7. self.cookiesService = CookiesFacebookService()
  8. self.options = Options
  9. @staticmethod
  10. def set_cookie_fb(cookies):
  11. list_keys_cookies = []
  12. for key, value in cookies.items():
  13. cookie_req = {
  14. "path": "/",
  15. "domain": ".facebook.com",
  16. "sameSite": "Lax",
  17. "name": key,
  18. "httpOnly": True,
  19. "secure": True,
  20. "value": value
  21. }
  22. list_keys_cookies.append(cookie_req)
  23. return list_keys_cookies
  24. def prepare_launch_browser(self):
  25. with sync_playwright() as playwright:
  26. return self.launch_playwright(playwright=playwright, options_browser=self.options)
  27. def launch_playwright(self, playwright, options_browser):
  28. user_cookies = self.cookiesService.getAccountFacebook()[0]["reqCookies"] # Here you can just put a valid facebook cookie in JSON format.
  29. browser = playwright.chromium.launch(**options_browser)
  30. page = browser.new_page()
  31. page.context.add_cookies(self.set_cookie_fb(user_cookies))
  32. page.goto("https://www.facebook.com/groups/javascript.react.node", wait_until='load')
  33. page.mouse.wheel(0, 1000)
  34. list_of_locators = page.locator('span[dir="auto"]:has-text("See more comments")').all()
  35. for index in list_of_locators:
  36. load_more_comments = index
  37. if load_more_comments is not None:
  38. page.on("response", self.filter_comment_response)
  39. load_more_comments.click() ##### ERROR TIMEOUT
  40. page.wait_for_load_state('load')
  41. self.counter += 1
  42. def filter_comment_response(self, response):
  43. if response.request.method == "POST" and "api/graphql" in response.request.url:
  44. try:
  45. text_body = response.body().decode('utf-8')
  46. final_text = re.sub(r'\n', ",", text_body)
  47. return final_text

字符串
这是主页中的元素,Ver más respuestas位于每个帖子中,就像红色圆圈中的这个一样。

通过点击span元素,它会显示出来,这是一个<div aria-labelledby=":r7q:" role="dialog"元素,它本身就像一个弹出窗口一样显示出来。


点击那个span查看更多评论抛出那个TimeoutError。希望能帮到我。

zz2j4svz

zz2j4svz1#

好吧,问题在于你的方法。如果我理解你的问题是,你想得到弹出数据,通过弹出我假设一个悬停元素。代码是在JavaScript中,但逻辑在Python中也是一样的。

  1. const list-of-locators=await page.locator('span[dir="auto"]:has-text("See more comments")');
  2. for(let i=0; i<list-of-locators.count();i++){
  3. await list-of-locators.nth(i).hover({ timeout: 3000, force: true }) // here we will hover over the first element of the list to get the popup locator
  4. const popUps= await list-of-locators.nth(i).locator('popup') // here we are inside the first element of list of locators and are getting popups inside that locator
  5. popUps.innerText() // here we can get the inner text of that popup
  6. }

字符串
我再一次假设你的弹出窗口是指悬停在元素上,如果你的意思是别的,那么在你的问题中加上这个细节,沿着一些应用程序的截图

相关问题