Using C# I am writing unit tests using Selenium (testing my react front end)…My html is:
<article class="company-details-container">
<div class="details-header">...</div>
<div class="details-header">...</div>
<section class="details-new-section>...</section>
<div class="details-header">...</div>
<div class="details-header">...</div>
<div class="details-header">...</div>
</article>
Section tag is the contents of each div, when you click on a div a new section appears with the divs contents… when the page loads the second div is already open, which is why the section tag is shown
I would like to click the first element inside the last div…
I am able to click the first element inside the first div by…
IWebElement elementa = driver.FindElement(By.CssSelector(".details-header"));
IJavaScriptExecutor executor1 = (IJavaScriptExecutor)driver;
executor1.ExecuteScript("arguments[0].click()", elementa);
I have tried IWebElement elementa = driver.FindElement(By.CssSelector(".details-header[0]"));
but this is not working.
ANy help please?
2
Answers
// Find the last div with class ‘details-header’
IWebElement lastDiv = driver.FindElement(By.CssSelector("div.details-header:last-of-type"));
// Find the first child element inside the last div
IWebElement firstElementInLastDiv = lastDiv.FindElement(By.CssSelector(":first-child"));
// Click on the first child element inside the last div
firstElementInLastDiv.Click();
You can use following
xpath
to click on last div, then first div of last div.