c# - PuppeteerSharp - 访问在新选项卡中打开的背景页面,作为常规页面
问题描述
我在使用从不和谐命令生成带有刷新令牌的 reddit 应用程序的程序时遇到问题。
我已经达到了一个点,我可以生成应用程序,获取所有相关信息,前往https://not-an-aardvark.github.io/reddit-oauth-helper/并从那里生成令牌,它将在新窗口中打开一个 reddit 确认页面。
我尝试以各种方式访问它,并经历了多种不同的方法,直到我开始使用 Target.PageAsync() 来获取页面。出于某种原因,Puppeteer 仅将页面视为 iFrame,并且仅在获取 Url 属性时才提供此链接 - https://www.redditmedia.com/gtm/jail?cb=8CqR7FcToPI - 这不会导致任何地方,但似乎与我在 HTML 中收集的第一个 iFrame 相关联。
我已经没有关于如何访问页面以按下一个按钮的想法,并且希望了解有关如何解决此问题或如何在不使用外部网站的情况下生成刷新令牌的任何想法或解决方案。
解决方案
又过了两个小时,我设法找到了解决方案。
由于 PuppeteerSharp 不愿意识别该页面,我只是在正确的时刻使用一个处理程序订阅了 Browser.TargetCreated,该处理程序在立即取消订阅后将记录最新的目标(在这种情况下,是一个调用 window.open() 的 javascript)和将发送者作为浏览器,然后尝试将页面放入数组并使用一些代码以确保它不会自行破坏,我终于找到了一个解决方案,经过三天零十二个多小时,我觉得有点愚蠢工作的。
对于可能遇到类似情况的任何人,这是使其最终起作用的代码片段:
// Bla bla bla code to crawl or do whatever on the main page.
// Immediately subscribe to the target created event with the event handler
// that will handle the background page once it has
// been triggered by a button, link, etc.
browser.TargetCreated += TargetCreatedEventHandler;
}
static async void TargetCreatedEventHandler(object sender, TargetChangedArgs e)
{
// Unsubscribe from the event to
// make sure there are no duplicate unnecessary calls that might break the code.
browser.TargetCreated -= TargetCreatedEventHandler;
// Since I know the sender is the Browser object,
// I cast it it to another Browser used inside the event handler.
Browser eventBrowser = (Browser) sender;
// Get all the pages from the event browser
// and assume the first page is background one (for now)
Page[] pages = await eventBrowser.PagesAsync();
Page page = pages[0];
int counter = 0;
// Iterate through the pages, check if they're the page you were just on,
// use an int to help you keep track of of indexes.
// If it isn't the page you were on, assign the Page object the page
// with the current counter index from pages.
// (basically make sure it doesn't break itself with the wrong order).
foreach (var item in pages)
{
if (item.Url != "Main Page URL HERE")
{
page = pages[counter];
break;
}
counter++;
}
// Do whatever you need to do on your background page here
}
推荐阅读
- r - 更改使用 stat_function 绘制的线的大小
- reactjs - ReactiveSearch - 无法从自动完成中强制选择(例如 strictSelection)?
- java - 2 同名的类,服务不同的目的
- html - CSS 格式不缩进 ng-repeat 输出的第二行
- javascript - 是否可以在警报框中获取网站设置的 http 标头?
- apache-kafka - 具有多个分区的 kafka 主题和具有多个队列侦听器的 ActiveMQ 有什么区别
- autodesk-forge - 加载多个模型时如何在查看器中通过 dbid 选择?
- vba - 从工作表函数传递范围时获取单元格内部颜色失败
- javascript - 关于匿名函数的概念和javascript中的调用
- node.js - docker 容器未启动 - 产生 Java enoent 错误