1

i have this element:

<label _ngcontent-c12="" tabindex="0" class="">
    <input _ngcontent-c12="" type="radio" class="ng-untouched ng-valid ng-dirty">
    <span _ngcontent-c12=""></span> TEST
  </label>

and how I can click on it using selenium C#? I try:

driver.FindElement(By.XPath("//label[text()='TEST']")).Click();

but it doesn't work

undetected Selenium
  • 151,581
  • 34
  • 225
  • 281

5 Answers5

3

The desired element is an Angular element so you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies as solutions:

  • XPath 1:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath("//div[@class='wrapper']")));
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[contains(., 'TEST')]//input"))).Click();
    
  • XPath 2:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath("//div[@class='wrapper']")));
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[normalize-space()='TEST']//input"))).Click();
    
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
0

Maybe, its because of missing space before 'TEST'. You may try something like driver.FindElement(By.XPath("//label[contains(text(),'TEST')]")).Click(); or driver.FindElement(By.XPath("//label[contains(.,'TEST')]")).Click();.

Also, reason maybe in absence of WebDriverWait

0

I think you can do more simple:

driver.FindElement(By.XPath("//label[contains(.,'TEST')]")).Click();

0

driver.FindElement(By.CssSelector(".ng-untouched.ng-valid.ng-dirty")).Click();

OrdinaryKing
  • 212
  • 2
  • 10
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Neo Anderson Sep 02 '20 at 16:42
0

Here I am trying to find particular text that is required to be clicked.

    IWebDriver driver = new ChromeDriver();  
    var label = driver.FindElements(by.TagName("span")).FirstOrDefault(ele=>ele.Text.Equals("TEST"));
    label.Click();

if above solution didn't work, But in Most cases inside Span tag , text are Displayed == false Then we need to use Actions Class.

Actions action = new Actions(driver);
action.MoveToElement(label).Click().Perform();
AmitKS
  • 122
  • 4