0

Sorry, my English is not good.

I use Selenium to get datas from web, Here is my code

var workGroups = e.WebDriver.FindElements(By.XPath("//div[@class='workgroup']"));

                    Console.WriteLine($"Item List: {workGroups.Count} Items");

        foreach (var workgroup in workGroups)
        {
            string workName = workgroup.FindElement(By.XPath("//div[@class='worktitle']/label")).Text;

            var detail = workgroup.FindElements(By.XPath("//div[@class='col-4 high']"));

            Console.WriteLine($"Item Name: {workName}, Number of Pictures: {detail.Count}");
        }

And this is the result: result

It seems to be catching the first data and all pictures, I use chromedriver to help me.

I don't know where it is wrong. Please help me, brothers and sisters. thank you very much.

4
  • which error/exception does it throw ? Commented Aug 19, 2018 at 9:53
  • No exceptions, but I caught 7 identical data. Commented Aug 19, 2018 at 10:27
  • Could you please provide HTML code? It seems like the issue is with the XPaths you are using and we cannot resolve it without the HTML data you are working on. Commented Aug 19, 2018 at 15:14
  • 1
    I can provide the website: henrychang.tw/Body/Works Commented Aug 19, 2018 at 16:06

1 Answer 1

1

Try to use:

string workName = workgroup.FindElement(By.XPath("./div[@class='worktitle']/label")).Text;
var detail = workgroup.FindElements(By.XPath("./div[@class='col-4 high']"));

I didn't test that but assuming from using workgroup element you would like to get only elements that are "inside" the workgroup element area. However, to do so you need to use current "folder" notation (./) instead of root element notation (//) which looking for elements starting from root node in your HTML document and actually going through the entire document.

Sign up to request clarification or add additional context in comments.

2 Comments

or workgroup.FindElement(By.XPath(".//div[@class='worktitle']/label"))
So it is because I use root element notation. I'll try, Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.