user4629659
user4629659

Reputation:

Get child element programmatically selenium c#

What I am trying to achieve is to programmatically open the first video on the web page which is enabled and displayed. I am getting the video elements by using Xpath search method and clicking on the first one available. What I want to achieve is to open that divider(video element) and inside of it search for another divider but specifically that one inside of the divider I already clicked on.

This is how I am trying to do it now:

 public void ClickOnTheProperty()
    {
        var isClicked = false;

        var properties = Driver.FindElements(By.XPath("//div[contains(@class, 'col-md-3 col-sm-3')]"));
        for (var i = 0; i < properties.Count; i++)
        {
            if (properties[i].Enabled && properties[i].Displayed)
            {
                var smallContainer =
                    properties[i].FindElements(By.XPath("//div[contains(@class, 'miniature lightingshow')]"));
                for (var j = 0; j < smallContainer.Count; j++)
                {
                    if (smallContainer[j].Displayed && smallContainer[j].Enabled)
                    {
                        var js = (IJavaScriptExecutor) Driver;
                        js.ExecuteScript("arguments[0].click()", smallContainer[i]);
                        isClicked = true;
                        break;
                    }
                }
            }
        }
        if (!isClicked)
        {
            throw new Exception("No property found");
        }
    }

Any suggestions? If you cannot understand what my problem is , or I did not explain very well, I will try to answer all the questions you need additionally.

Upvotes: 3

Views: 7093

Answers (1)

Arran
Arran

Reputation: 25056

Your issue is that XPath (not Selenium) requires a little more of a 'push' to tell it about scopes.

Your solution is to change your second XPath from:

//div[contains(@class, 'miniature lightingshow')]"

to:

.//div[contains(@class, 'miniature lightingshow')]"

The . telling XPath 'search within my current parent'.

Upvotes: 2

Related Questions