2

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
creed
  • 1,409
  • 1
  • 9
  • 18

1 Answers1

1

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I've tried this and have gotten this error: OpenQA.Selenium.WebDriverTimeoutException: 'Timed out after 20 seconds' with the same inner exception: NoSuchElementException: no such element: Unable to locate element, here is the website: https://www.keyfood.com/store/keyFood/en/printable-coupons, you can test it for yourself, it's the "Clip All" button near the bottom of the page. – creed Jul 17 '20 at 20:34
  • @africohal Checkout the updated answer and let me know the status. – undetected Selenium Jul 17 '20 at 21:02
  • This time I got this exception: OpenQA.Selenium.WebDriverTimeoutException: 'Timed out after 20 seconds' followed by this inner exception: InvalidSelectorException: invalid selector: An invalid or illegal selector was specified which occured on this line: new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("//input.selectall-chk#couponsinc-gallery-selectall"))).Click(); – creed Jul 17 '20 at 21:17
  • @africohal My bad, it was a typo, fixed it, please check now both the locators and let me know the status. – undetected Selenium Jul 17 '20 at 21:20
  • 1
    Awesome! The solution seems to work and I've marked it as the soltuion but I still don't understand why my simple code doesn't work as it definitely worked for a different button on a different website. – creed Jul 17 '20 at 21:23
  • @africohal I tried my best to provide you all the reference in the form on embedded links. Happy coding – undetected Selenium Jul 17 '20 at 21:25