我正试图从网站的节点中获取电话号码。出于某种原因,当我在chrome中检查节点时,元素内部的实际数字是不可见的。以下是我尝试从https://tempophone.com/获取号码的网站。我是在检查错误的元素,还是通过访问节点从网站上获取电话号码是不可能的。这是我的代码,我使用的是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");
答案 0 :(得分:2)
首先,该节点内没有文字。
第二个你想要的是这个。
string s = doc.DocumentNode.SelectNodes("//*[@id='temporary-phone']")[0].GetAttributeValue("value", "false");
第三。这将始终返回“正在加载...”。因为使用javascript更新/更改了节点中的属性“value”。当您使用HtmlWeb或HttpWebRequest时,您将始终获得页面的来源。如果您希望能够将动态内容加载到HtmlDocument中,则需要将WebBrowser或Selenium与WebDriver一起使用。
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);