Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Selenium Chromedriver时无法定位元素';C里有什么#_C#_Selenium_Xpath_Css Selectors_Webdriverwait_Checkbox - Fatal编程技术网

C# 使用Selenium Chromedriver时无法定位元素';C里有什么#

C# 使用Selenium Chromedriver时无法定位元素';C里有什么#,c#,selenium,xpath,css-selectors,webdriverwait,checkbox,C#,Selenium,Xpath,Css Selectors,Webdriverwait,Checkbox,我试图让我的程序点击一个按钮,但我收到错误 我的代码: 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: {&

我试图让我的程序点击一个按钮,但我收到错误

我的代码:

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
,但没有任何效果。我做错了什么?

以文本为剪辑的所有剪辑都是
,因此您必须:

  • 使所需帧可用并切换到该帧

  • 使所需元素可单击

  • 您可以使用以下任一选项:

  • 使用
    CSS\u选择器

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


参考文献 您可以在以下内容中找到一些相关讨论:


我尝试过这个方法,但出现了以下错误:OpenQA.Selenium.WebDriverTimeoutException:“20秒后超时”,内部异常相同:NoTouchElementException:没有这样的元素:找不到元素,这是网站:,您可以自己测试,这是“全部剪辑”按钮。@africohal签出更新的答案并告诉我状态。这一次我得到了以下异常:OpenQA.Selenium.WebDriverTimeoutException:“20秒后超时”,然后是内部异常:InvalidSelectorException:无效选择器:指定的选择器无效或非法,发生在此行:新WebDriverWait(驱动程序,TimeSpan.FromSeconds(20))。直到(ExpectedConditions.ElementToBickable(通过.CssSelector(//input.selectall chk#couponsinc gallery selectall))。单击()@africohal我的错,这是一个拼写错误,修复了它,现在请检查两个定位器,让我知道状态。太棒了!这个解决方案似乎有效,我已经将其标记为soltuion,但我仍然不明白为什么我的简单代码不起作用,因为它确实适用于不同网站上的不同按钮。