Reputation: 1427
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
Reputation: 193078
The checkbox 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:
You can find a couple of relevant discussions in:
Upvotes: 1