javascript - 如何使用 Selenium WebDriver 获取 disqus 评论 iframe 数据

标签 javascript c# selenium iframe disqus

我一直在尝试使用 Selenium WebDriver (Chrome) 加载页面并获取 disqus 评论线程。请查看代码。

public static void Main(string[] args)
        {
            var driver = new ChromeDriver();
            driver.Navigate().GoToUrl("http://nation.com.pk/blogs/05-Apr-2016/should-qandeel-baloch-s-strip-tease-really-be-a-rallying-cause-for-liberalism");
            var userNameField = driver.FindElementById("disqus_thread").GetAttribute("outerHTML");
            Console.WriteLine(userNameField);
            Console.Read();
        }

它给出以下输出。

<div id="disqus_thread"><iframe id="dsq-app2" name="dsq-app2" allowtransparency="true" frameborder="0" scrolling="no" tabindex="0" title="Disqus" width="100%" src="http://disqus.com/embed/comments/?base=default&amp;version=af1a2e2611136ef6c314afec2806cef3&amp;f=nawaiwaqt&amp;t_u=http%3A%2F%2Fnation.com.pk%2Fblogs%2F05-Apr-2016%2Fshould-qandeel-baloch-s-strip-tease-really-be-a-rallying-cause-for-liberalism&amp;t_d=Should%20Qandeel%20Baloch%E2%80%99s%20%E2%80%98striptease%E2%80%99%20really%20be%20a%20rallying%20cause%20for%20liberalism%3F&amp;t_t=Should%20Qandeel%20Baloch%E2%80%99s%20%E2%80%98striptease%E2%80%99%20really%20be%20a%20rallying%20cause%20for%20liberalism%3F&amp;s_o=default" style="width: 1px !important; min-width: 100% !important; border: none !important; overflow: hidden !important; height: 652px !important;" horizontalscrolling="no" verticalscrolling="no"></iframe></div>

这与我的期望相反,因为我希望在这个阶段获得评论 html。如何修改此代码以获得评论线程? PS:原始页面源码中没有iframe的指示。

最佳答案

这就是我如何获取 iframe 源代码以进行进一步处理。希望它对其他人有帮助。

var driver = new ChromeDriver();
            int tmp = 1;
            foreach(string file in File.ReadLines(@"bla bla bla\Bloglinks.txt"))
            {
                driver.Navigate().GoToUrl(file);
                driver.SwitchTo().Frame("dsq-app2");
                var userNameField = driver.PageSource;
                File.WriteAllText(@"bla bla bla\Comments\"+tmp+".html", userNameField);
                Console.WriteLine(file);
                tmp++;
            }
            driver.Close();

关于javascript - 如何使用 Selenium WebDriver 获取 disqus 评论 iframe 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36810584/

相关文章:

java - AES 256 java 和 .Net 兼容加密和解密?

java - 如何在selenium webdriver中定位动态变化的图像?

python - “phantomjs”可执行文件可能具有错误的权限

c# - 如何定义可以返回 DataTable 或 List <T> 的方法?

c# - 在 ASP.NET c# 中设置 sqldatasource select 语句时防止 SQL 注入(inject)

python - urllib3.exceptions.ProtocolError : ('Connection aborted.' ,错误(10054, 'An existing connection was forcibly closed by the remote host'))

javascript - 无法读取React中未定义的属性 'forEach'

javascript - 如何获取事件源元素

javascript - 如何禁用 YUI2 静默失败?

javascript - 通过 JS 的随机 CSS