fuzzymind
fuzzymind

Reputation: 43

How to fill a form inside an iframe with Puppeteer-Sharp

I am trying to scrape Taobao website with Puppeteer Sharp.

Here is the code:

private static async Task SurfWithPuppeteer()
    {
        var options = new LaunchOptions{ Devtools  = true };

        Console.WriteLine("Downloading chromium");

        await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
        Console.WriteLine("Navigating to Hacker News");
        using (var browser = await Puppeteer.LaunchAsync(options))
        using (var page = await browser.NewPageAsync())
        {
            page.DefaultNavigationTimeout = 50000;
            await page.GoToAsync("https://login.tmall.com/?spm=875.7931836/B.a2226mz.1.66144265pHmhvt&redirectURL=https%3A%2F%2Fwww.tmall.com%2F");
            var frameElement= await page.QuerySelectorAsync("#J_loginIframe");
            //var frameElement = await page.QuerySelectorAsync("div#mallPage iframe");
            //var frameElement = await page.Frames.Select(f=>f.QuerySelectorAsync("#J_loginIframe")).FirstOrDefault();
            var frame = await frameElement.ContentFrameAsync();
            var frameContent = await frame.GetContentAsync();
            await frame.TypeAsync("#TPL_username_1", "compuwizpiyu");
            await frame.TypeAsync("#TPL_password_1", "Priyanka24$");
            var btn = await frame.QuerySelectorAsync("#J_SubmitStatic");

            await btn.ClickAsync();
            var res= await frame.WaitForNavigationAsync();
           var t=  await frame.GetContentAsync();
            //var url = page.Url;
        }
    }

But I am unable to navigate to the frame that has the login form (frame has no name, only src and id).

I have tried to check the frames with page.Frames, but since the iframes have no name, difficult to find the correct frame I am looking for. I have tried couple of other options too like :

var frameElement = await page.QuerySelectorAsync("div#mallPage iframe");
var frameElement = await page.Frames.Select(f=>f.QuerySelectorAsync("#J_loginIframe")).FirstOrDefault()

But still unable to get the intended frame. Please help me with what is wrong here.

Upvotes: 1

Views: 2248

Answers (1)

Sygel4456
Sygel4456

Reputation: 94

This may be due to CORS.

Try the following code.

var options = new LaunchOptions
{
    Devtools = true,
    Args = new[]
    {
        "--disable-web-security",
        "--disable-features=IsolateOrigins,site-per-process",
    },
};

Upvotes: 2

Related Questions