How do i increment the data which has a [@id=”app”]/article/div/div[4]/div[2]/div[1]/ol/li[1]/div — li[1] <– increment each foreach so the data doesnot duplicate from the 1st row list it also sometimes gets error like No such elements found if the element xpath change and it’s still in one list how do i do that?
var DataLst = driver.FindElements(By.XPath("//*[@id="app"]/article/div/div[4]/div[2]/div[1]/ol/li"));
int number = 1;
try
{
foreach (var name in DataLst)
{
var GetName = name.FindElement(By.XPath("//*[@id="app"]/article/div/div[4]/div[2]/div[1]/ol/li[" + number + "]/div/div/div[1]/div[1]/div[1]/div/div[1]/a/h2"));
var GetProf = name.FindElement(By.XPath("//*[@id="app"]/article/div/div[4]/div[2]/div[1]/ol/li[" + number + "]/div/div/div[1]/div[1]/div[1]/div/p"));
var GetAddr = name.FindElement(By.XPath("//*[@id="app"]/article/div/div[4]/div[2]/div[1]/ol/li[" + number + "]/div/div/div[1]/div[2]/div[1]/div[2]/p"));
if (!string.IsNullOrWhiteSpace(GetName.Text))
{
Console.WriteLine(GetName.Text);
Console.WriteLine(GetProf.Text);
Console.WriteLine(GetAddr.Text);
Console.WriteLine(Environment.NewLine);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);
}
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
number++;
}
}
catch (Exception ex) { }
OpenQA.Selenium.NoSuchElementException: ‘no such element: Unable to locate element: {“method”:”xpath”,”selector”:”//*[@id=”app”]/article/div/div[4]/div[2]/div[1]/ol/li[4]/div/div/div[1]/div[1]/div[1]/div/div[1]/a/h2″}
(Session info: chrome=125.0.6422.142); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception’
Probably maybe the XPath is different but they are in the same class as i tried to inspect the div class name of the whole list.
if possible can you give me just a short code reference of how to proper scrape data list of a div class value of h2 element inside a ul li list data just throw me a bone or vision of how it is done i can then think of a way for my code.
I tried declaring an Int num variable count and increment it in the foreach loop and added it to the XPath li looking like this –> li [” + num + “] it still worked until it reach row 3 and then it says no such element error not taking all the 26 data in the list. unlike the li only but if i used this one it just duplicate the record on the 1st row list data for 26 times.
Paowix Aks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.