0

I'm new to c# and I'm having problems locating elements. I don't quite understand how to find relative xpaths. I'm trying to locate an element. My code is listed below:

IWebElement webElement = driver.FindElement(By.XPath("Nothing I put here works"));
Thread.Sleep(1000);
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;

Here is what I'm trying to find:

<li _ngcontent-c2="" class="header-item person-search ng-tns-c2-1 ng-star-inserted" ngbdropdown="" ngbtooltip="Person Search" placement="left">
    <a _ngcontent-c2="" class="dropdown-toggle dropdown-toggle" aria-haspopup="true" href="javascript:void(0)" ngbdropdowntoggle="" aria-expanded="false">
        <span _ngcontent-c2="" class="fa fa-search-plus block"></span>
    </a>
</li>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 4
    I would highly suggest you replace `"Nothing I put here works"` with an actual attempt you've made (and maybe show more than one attempt to be sure people know what was tried). That way, it's easier to pintpoint the problem in your method call. – Mat Feb 26 '19 at 22:56
  • Uploading the entire HTML for your page would be helpful, I think. – C. Peck Feb 27 '19 at 06:05

1 Answers1

0

The desired element is an Angular element so to locate the element you have to induce WebDriverWait with ExpectedConditions as ElementToBeClickable Method (By) and you can use either of the following solutions:

  • CssSelector:

    IWebElement webElement = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("li.header-item.person-search.ng-star-inserted[ngbtooltip='Person Search']>a.dropdown-toggle>span.fa.fa-search-plus.block"))).Click();
    
  • XPath:

    IWebElement webElement = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//li[@class='header-item person-search ng-tns-c2-1 ng-star-inserted' and @ngbtooltip='Person Search']//a[@class='dropdown-toggle dropdown-toggle']/span[@class='fa fa-search-plus block']")));
    

Update

If you are using nuget, search for DotNetSeleniumExtras.WaitHelpers and import that namespace into your class and you can do this:

new WebDriverWait(driver, new TimeSpan(0, 0, 30)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("elementID")));

You can find a relevant discussion in C# Selenium 'ExpectedConditions is obsolete'

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • This helped. If I use the CssCelector method, the Click() has to be moved underneath that line like "webElement.Click();" otherwise it gives an error cannot convert void to Selenium.QA, etc...And both statements work as long as the Click() is moved underneath like stated above. One question I have is this....It is stating that ExpectedConditions is obsolete and will be deprecated. What can that be changed to for going forward? – christopherhe1 Feb 27 '19 at 14:41
  • @christopherhe1 Checkout the updated answer and let me know the status. – undetected Selenium Feb 27 '19 at 15:02