Reputation: 111
I gotta verify if a button is "selected", if it is, then we can procede the test, if not, select it.
I tried the bellow code. What is the proper way to set up this?
External HTML: I need to verify the class: see if it has the string "selected", if this div is selected then continue, if not, click on it to be "Selected".
<div class="list-toggle uppercase management_item newFeature selected" ng-class="{ 'selected': viewModel.selectedActionGroup == 'DistributionGroup', 'newFeature': i.isNew, 'standardFeature': !i.isNew}">
Code:
var element = driver.FindElement(By.CssSelector(".col-md-3 > div:nth-child(1) > div:nth-child(3) > a:nth-child(1) > div:nth-child(1) > span:nth-child(2)"));
if (element.GetAttribute("class").Contains("selected"))
{
return;
}
else
{
element.Click();
}
Also I tried this:
if (element.GetAttribute("class").("selected", StringComparison.OrdinalIgnoreCase) == -1)
{
element.Click();
}
else
{
return;
}
PD: If I hover over the "Else" statement it says is redundant, why is this?
The issue is that i made the verification but the program clicks on it either way.
Upvotes: 0
Views: 207
Reputation: 7563
Try change the locator by xpath
:
var element = driver.FindElement(By.XPath("//*[contains(@class,'list-toggle uppercase management_item newFeature')]"));
if (element.GetAttribute("class").Contains("selected"))
{
return;
}
else
{
element.Click();
}
Upvotes: 1
Reputation: 443
You can check value of any attribute of element like this
element.GetAttribute("ng-class").Contains("selected");
Or you may simple run search by this condition
Driver.FindElement(By.Xpath(".//div[contains(@ng-class,'selected')]");
Upvotes: 0