xvrh / puppeteer-dart

A Dart library to automate the Chrome browser over the DevTools Protocol. This is a port of the Puppeteer API
BSD 3-Clause "New" or "Revised" License
236 stars 59 forks source link

Simple hyperlink click-and-wait not working #314

Closed TheFriendlyCoder closed 6 months ago

TheFriendlyCoder commented 6 months ago

I am testing out Puppeteer for the first time, and thought I'd try a simple scenario: I want to open a web page that has a hyperlink on it, click the hyperlink, and wait for the page to refresh. Below is the sample code I am using:

var browser = await puppeteer.launch();
var myPage = await browser.newPage();
myPage.setUserAgent(
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
await myPage.goto('https://www.simplii.com');
var result = await myPage.clickAndWaitForNavigation(".joinSimpliiLink");
print(myPage.url);
print(result);

I expect the value of result to get printed, which should give me the success status from the navigation operation, and the myPage variable to be updated to point to the target URL for the hyperlink selected. What I get instead is an exception:

Unhandled exception:
Context is disposed

I'm running on a Mac with the last OS, Dart v3.3, and the latest version of puppeteer-dart in case this helps isolate the problem.

TheFriendlyCoder commented 6 months ago

I also tried a variation of this using the waitForSelector method as well, with similar results:

print("Click and wait");
var result = await myPage.waitForSelector(".joinSimpliiLink",
    visible: true, hidden: false);
if (result == null) {
    print("Error not found");
    return;
}
print("Clicking...");
await Future.wait([myPage.waitForNavigation(), result!.click()]);
print(myPage.url);
print(result);

which produces the output:

Click and wait
Clicking...
Unhandled exception:
Context is disposed

which tells me the selector lookup works, but the click/waitForNavigation doesn't.

TheFriendlyCoder commented 6 months ago

One more similar variation for comparison:

print("Click and wait");
var result = await myPage.waitForSelector(".joinSimpliiLink",
    visible: true, hidden: false);
print("clicking...");
await result!.click();
print("navigating...");
var result2 = await myPage.waitForNavigation();
print(result2.status);
print(myPage.url);

which produces:

Click and wait
clicking...
navigating...
Unhandled exception:
Context is disposed

Which further isolates the problem to the waitForNavigation method.

xvrh commented 6 months ago

Intriguing... Luckily, there is this open pull request to address that: https://github.com/xvrh/puppeteer-dart/pull/312 I'll have a look to merge it.

xvrh commented 6 months ago

Can you try v3.11.0?

TheFriendlyCoder commented 6 months ago

Seems to work now. Thanks for the quick fix!