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
  • 183,867
  • 41
  • 278
  • 352
  • 1
    your TEST text has space before it or it's a typo? Try label[contains(.,'TEXT')] or label[normalize-space(.)='TEXT'] – Somber Sep 19 '19 at 12:02
  • *Typo* + *Why is this code not working must include...* Voting to close – Nouman Sep 19 '19 at 12:04
  • It helps! But:OpenQA.Selenium.ElementClickInterceptedException : element click intercepted: Element is not clickable at point (974, 547). Other element would receive the click:
    ...
    (Session info: chrome=76.0.3809.132)
    – Adrian Gielata Sep 19 '19 at 12:54
  • I think need to click imput, how? – Adrian Gielata Sep 19 '19 at 12:54

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
  • 183,867
  • 41
  • 278
  • 352
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

IvanMikhalka
  • 184
  • 10
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
  • 411
  • 1
  • 7
  • 16
  • 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
  • 132
  • 4