Home > Enterprise >  NoSuchElementException: no such element: Unable to locate element during web scrape
NoSuchElementException: no such element: Unable to locate element during web scrape

Time:05-27

I am trying to scrape this website: https://www.tripadvisor.com/Restaurants-g293718-Algiers_Algiers_Province.html

I used this code:

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");
}

But I got this error message:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//a[@class='bHGqj Cj b']"} (Session info: headless chrome=101.0.4951.67)

CodePudding user response:

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

Change

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

To

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

CodePudding user response:

Another approach is carving text out of the element using the class.

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