Code Monkey home page Code Monkey logo

Comments (12)

miyakogi avatar miyakogi commented on May 14, 2024 1

Your solution is correct.

The problem of your first code is:

await page.goto("https://github.com")
await page.click('.HeaderMenu [href="/features"]')  # start navigation to the feature page here
await page.waitForNavigation()  # waiting for the "next" navigation from the feature page

To avoid this situation, you need to start click and waitForNavigation (almost) simultaneously, as your solution.

I will update document about page.click() by the next release.

from pyppeteer.

miyakogi avatar miyakogi commented on May 14, 2024 1

You need to wait for page contents loaded.
Use waitForSelector method before click.

print(await result_page.title())
await result_page.waitForSelector('a.ng-binding')
await result_page.click('a.ng-binding')

from pyppeteer.

zhanghaofei avatar zhanghaofei commented on May 14, 2024

Seems to have found a way to achieve, but I do not understand, do not know whether the correct, I hope to get your guidance, thank you
solution:

await asyncio.wait([page.click('.HeaderMenu [href="/features"]'), page.waitForNavigation()])

from pyppeteer.

tonggang avatar tonggang commented on May 14, 2024

sorry guys,i found the solution still not work in my case. any idea for this?

async def main():
browser = await launch({"headless":False})
page = await browser.newPage()
await page.goto('http://wsjs.saic.gov.cn')
await page.screenshot({'path': 'example.png'})
await page.click('#txnS02',{"waitUntil":"networkidle0"})
title = await page.title()
if title is None or title =="":
await page.reload({"waitUntil":"networkidle0"})
print (await page.title() + str(len(await browser.pages())))
await page.screenshot({'path': 'example1.png'})
await page.focus("input[name='request:sn']")
await page.type("input[name='request:sn']","26716759",{"delay": 50})
await page.screenshot({'path': 'example2.png'})
await asyncio.wait([page.click('#_searchButton'), page.waitForNavigation()])
print (await page.title())
# await browser.close()
asyncio.get_event_loop().run_until_complete(main())

from pyppeteer.

miyakogi avatar miyakogi commented on May 14, 2024

Since page.click does not accept waitUntil option, this line is incorrect.

await page.click('#txnS02',{"waitUntil":"networkidle0"})

So you need to use await asyncio.wait([page.click(...), page.waitForNavigation()]) here, too.

from pyppeteer.

miyakogi avatar miyakogi commented on May 14, 2024

Updated document of Page.click()

from pyppeteer.

tonggang avatar tonggang commented on May 14, 2024

actually it's working for me at that line ,but still got the Navigation Timeout Exceeded: 30000 ms exceeded at line await asyncio.wait([page.click('#_searchButton'), page.waitForNavigation()])

from pyppeteer.

miyakogi avatar miyakogi commented on May 14, 2024

Thank you for details.
I reproduced the error.
I will try to fix it.

from pyppeteer.

miyakogi avatar miyakogi commented on May 14, 2024

I understand the problem.
Since page.click('#_searchButton') opens a new tab, navigation event is fired in the different page instance.
So you need to watch targetcreated event on the browser.

Try the below code:

import asyncio
from pyppeteer import launch

async def main() -> None:
    browser = await launch({"headless": False, "args": ['--no-sandbox']})
    page = await browser.newPage()
    await page.goto('http://wsjs.saic.gov.cn')
    await page.screenshot({'path': 'example.png'})
    await asyncio.wait([page.waitForNavigation(), page.click('#txnS02')])

    title = await page.title()
    if title is None or title == "":
        await page.reload({"waitUntil": "networkidle0"})

    print(await page.title() + str(len(await browser.pages())))
    await page.screenshot({'path': 'example1.png'})
    await page.focus("input[name='request:sn']")
    await page.type("input[name='request:sn']", "26716759", {"delay": 50})
    await page.screenshot({'path': 'example2.png'})

    result_page_future = asyncio.get_event_loop().create_future()
    browser.once('targetcreated',
                 lambda target: result_page_future.set_result(target))
    await page.click('#_searchButton'),
    result_page = await (await result_page_future).page()
    print(await result_page.title())
    # await browser.close()

asyncio.get_event_loop().run_until_complete(main())

output is:

商标综合检索2
商标检索结果

from pyppeteer.

tonggang avatar tonggang commented on May 14, 2024

hi bro, but seems i'm not able to click the link inside the table. Means if this page is dynamic loading how can i access the content?Thanks

import asyncio
from pyppeteer import launch

async def main() -> None:
browser = await launch({"headless": False, "args": ['--no-sandbox']})
page = await browser.newPage()
await page.goto('http://wsjs.saic.gov.cn')
await page.screenshot({'path': 'example.png'})
await asyncio.wait([page.waitForNavigation(), page.click('#txnS02')])

title = await page.title()
if title is None or title == "":
    await page.reload({"waitUntil": "networkidle0"})

print(await page.title() + str(len(await browser.pages())))
await page.screenshot({'path': 'example1.png'})
await page.focus("input[name='request:sn']")
await page.type("input[name='request:sn']", "26716759", {"delay": 50})


result_page_future = asyncio.get_event_loop().create_future()
browser.once('targetcreated',
             lambda target: result_page_future.set_result(target))
await page.click('#_searchButton')
result_page = await (await result_page_future).page()

print(await result_page.title())
await result_page.click("a.ng-binding")  #it does not work

await browser.close()

asyncio.get_event_loop().run_until_complete(main())

from pyppeteer.

tonggang avatar tonggang commented on May 14, 2024

Thank u bro!!

from pyppeteer.

SIMPNDEV avatar SIMPNDEV commented on May 14, 2024

Hi @miyakogi , could you give this post https://stackoverflow.com/questions/60763644/cant-go-on-clicking-on-the-next-page-button-while-scraping-certain-fields-from a shot in case there is any solution you would like to offer. The post that I created in there is about pyppeteer. Thanks.

from pyppeteer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.