提问者:小点点

NoSuchElementException:没有这样的元素:在网页抓取期间无法定位元素


我试图刮这个网站:https://www.tripadvisor.com/Restaurants-g293718-Algiers_Algiers_Province.html

我使用了这段代码:

driver.Navigate().GoToUrl("https://www.tripadvisor.com/Restaurants-g293718-Algiers_Algiers_Province.html");
        
var collection = driver.FindElements(By.XPath("//div[@class='cauvp Gi o']"));

foreach (var item in collection)
{
    string name = item.FindElement(By.XPath(".//a[@class='bHGqj Cj b']")).Text;
    string location = "Algiers";
    string type = item.FindElement(By.XPath(".//div[@class='bhDlF bPJHV eQXRG']/span/span")).Text;
    IWebElement img= item.FindElement(By.XPath(".//div[@class='bdLpT w carousel fjXXd ddFHE']/div/ul/li/div"));
    
    string image = img.GetCssValue("background-image").Replace("url(\"",string.Empty).Replace("\")",string.Empty);
    string link = item.FindElement(By.XPath(".//a[@class='bHGqj Cj b']")).GetAttribute("href");
}

但是我收到了这个错误消息:

NoSuchElementException:没有这样的元素:无法定位元素:{"method":"xpath","selector":".//a[@class='bHGqj Cj b']"}(会话信息:无头chrome=101.0.4951.67)


共2个答案

匿名用户

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

改变

 string name = item.FindElement(By.XPath(".//a[@class='bHGqj Cj b']")).Text;

 string name = item.FindElement(By.XPath("//[@class='bHGqj Cj b']")).Text;

匿名用户

另一种方法是使用类从元素中雕刻文本。

foreach(var item in collection(".//a[@class='bHGqj Cj b']//text()[normalize-space() and not(ancestor::span)]")) {
string name = item.InnerText.Trim();
}