当它在 C# 中时,无法使用 Selenium Chromedriver 定位元素

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

我试图让我的程序点击按钮,但我收到错误消息。

我的代码:

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

错误:

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"}

页面上按钮的代码如下:

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

我也尝试过使用 FindElementByClassName 但没有任何效果。我做错了什么?

的文本为 Clip All<iframe> 所以你必须:

  • 诱导 WebDriverWait 所需的 帧可用并切换到它.

  • 诱导 所需的 元素可点击.

  • 您可以使用以下任一方式:

  • 使用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();
    
  • 使用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();
    
  • 浏览器快照:


参考

您可以在以下位置找到一些相关讨论: