creed
creed

Reputation: 1427

Unable to locate element using Selenium Chromedriver when it's there in C#

I am trying to get my program to click on a button however I receive errors.

My code:

driver.FindElementById("couponsinc-gallery-selectall").Click();

Error:

An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll
no such element: Unable to locate element: {"method":"css selector","selector":"#\couponsinc\-gallery\-selectall"}

Here is the code of the button on the page:

<div class="selectall">
            <input type="checkbox" class="selectall-chk" id="couponsinc-gallery-selectall">
            <label for="couponsinc-gallery-selectall">Clip All</label>
        </div>

I've also tried my using FindElementByClassName but nothing is working. What am I doing wrong?

Upvotes: 2

Views: 220

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193078

The with text as Clip All an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    ((IJavaScriptExecutor)driver).ExecuteScript("return scrollBy(0, 800);");
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.CssSelector("iframe[title='Coupons']"));
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.selectall-chk#couponsinc-gallery-selectall"))).Click();
    
  • Using XPATH:

    ((IJavaScriptExecutor)driver).ExecuteScript("return scrollBy(0, 800);");
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.XPath("//iframe[@title='Coupons']"));
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='selectall-chk' and @id='couponsinc-gallery-selectall']"))).Click();
    
  • Browser Snapshot:

keyfoods


Reference

You can find a couple of relevant discussions in:

Upvotes: 1

Related Questions