0

The following is the source code that I am attempting to work with. I have attempted to use methods such as:

driver.FindElement(By.XPath("//span[@data-bind = 'text: Caption']")).Click();
driver.FindElement((By.XPath("//*[contains(text(),'document')]"))).Click();

All the conventional selenium methods that I have been using in my project have not been able to find anything.

<div class="caption" data-bind="css: { 'doc-downloader-wrapper': $data.IsDocument() }">
  <a data-bind="css: { 'protected': IsProtectedDocument(), 'doc-downloader': $data.IsDocument(), 'no-       details': !$data.HasDetails() }, attr: { href: Link, target: $data.IsLinkDocument() ? '_blank' :         '_self' }">
    <span data-bind="text: Caption"></span>
  </a>
</div>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Possible duplicate of [Retrieving Text between Text in Selenium C#](https://stackoverflow.com/questions/14057699/retrieving-text-between-spantext-span-in-selenium-c-sharp) – itsMasoud Nov 06 '19 at 21:02
  • Have you tired using `Actions`? – King11 Nov 06 '19 at 21:10
  • I'm not acquainted with Selenium C# so this may be a stupid suggestion. Can you select with a CSS selector? e.g. `document.querySelector('span[data-bind="text: Caption"]')` – customcommander Nov 06 '19 at 21:20

1 Answers1

1

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

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.caption[data-bind*='doc-downloader-wrapper']>a[data-bind*='IsProtectedDocument']>span[data-bind$='Caption']"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='caption' and contains(@data-bind, 'doc-downloader-wrapper')]/a[contains(@data-bind, 'IsProtectedDocument')]/span[contains(@data-bind, 'Caption')]"))).Click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352