Html 节点内的信息不可见

Information inside Html Node not visible

我正在尝试从网站的节点中获取 phone 号码。出于某种原因,当我检查 chrome 中的节点时,元素内部的实际数字不可见。这是我试图从中获取号码的网站:https://tempophone.com/。我是在检查错误的元素还是无法通过访问节点从网站上获取 phone 号码。这是我的代码,我正在使用 htmlAgilityPack:

        string url = "https://tempophone.com/";
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(url);

        var phoneNumber = doc.DocumentNode.SelectNodes("//*[@id=\"temporary - phone\"]")[0].InnerText;
        if(phoneNumber != null)
            Console.WriteLine(phoneNumber);
        else
            Console.WriteLine("null");

这是检查元素的屏幕截图,您可以看到那里没有 phone 数字:

首先,该节点内没有文本。

第二你要的是这个

string s = doc.DocumentNode.SelectNodes("//*[@id='temporary-phone']")[0].GetAttributeValue("value", "false");

第三。这将始终 return "Loading..."。因为节点中的属性'value'被javascript使用updated/changed。当您使用 HtmlWeb 或 HttpWebRequest 时,您将始终获得页面的来源。如果您希望能够将动态内容加载到您的 HtmlDocument 中,您需要将 WebBrowser 或 Selenium 与 WebDriver 结合使用。

如何使用 Selenium 和 FirefoxDriver

        var driver = new FirefoxDriver();
        driver.Navigate().GoToUrl("https://tempophone.com/");
        Thread.Sleep(2000);
        driver.FindElement(By.XPath("//button[@id='check-phone']")).Click();
        string s = driver.FindElement(By.XPath("//h1[@id='phone-number']")).GetAttribute("data-phone-number");
        Console.WriteLine("Here: " + s);

或者你可以打电话给他们 API

https://tempophone.com/api/v1/phones/random