From b85581bccf8607b76e77bb9bea28fd8e3f4b1824 Mon Sep 17 00:00:00 2001 From: Onur YILDIZ <41267854+onur-yildiz@users.noreply.github.com> Date: Tue, 9 Aug 2022 00:07:59 +0300 Subject: [PATCH] fix: finding uploaded audio link --- Puppeteer/Puppet.cs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Puppeteer/Puppet.cs b/Puppeteer/Puppet.cs index 6fb5037..f2b7f8a 100644 --- a/Puppeteer/Puppet.cs +++ b/Puppeteer/Puppet.cs @@ -30,10 +30,12 @@ public static async Task CutAndUploadAudio(string name, string url, stri var client = await page.Target.CreateCDPSessionAsync(); var mouse = new Mouse(client, page.Keyboard); + var uniqueName = name + Guid.NewGuid().ToString(); + Console.Clear(); await Login(page); - await Upload(page, name); - return await ExtractURL(page); + await Upload(page, uniqueName); + return await ExtractURL(page, uniqueName); } catch (Exception ex) { @@ -63,7 +65,7 @@ static async Task Login(Page page) static async Task Upload(Page page, string name) { Console.WriteLine("Uploading audio..."); - await page.TypeAsync("#id_name", name + Guid.NewGuid().ToString()); + await page.TypeAsync("#id_name", name); var fileChooserDialogTask = page.WaitForFileChooserAsync(); await Task.WhenAll(fileChooserDialogTask, page.ClickAsync("input[name=sound]")); var fileChooser = await fileChooserDialogTask; @@ -78,14 +80,15 @@ static async Task Upload(Page page, string name) Console.WriteLine("Uploaded Audio."); } - static async Task ExtractURL(Page page) + static async Task ExtractURL(Page page, string name) { Console.WriteLine("Navigating to the audio page..."); - await page.EvaluateFunctionAsync(@" - () => { - const audioList = document.querySelectorAll('a.instant-link'); - audioList[audioList.length -1].click(); - } + await page.EvaluateFunctionAsync($@" + () => {{ + const audioList = document.querySelectorAll('a.instant-link'); + const link = Array.from(audioList).find(a => a.innerText === '{name}') + link.click(); + }} "); //await page.WaitForNavigationAsync();