简体   繁体   English

无法使用Selenium C#单击复选框元素

[英]Can't click checkbox element with Selenium C#

I have problem clicking on a checkbox. 我在单击复选框时遇到问题。 When I try to use the webdriver click() function it says that it can't locate the element, then I try to use a JS execution which I test in the browser console and it work, but when I try to execute it through the selenium webdriver, it don't work again. 当我尝试使用webdriver click()函数时,它说它找不到该元素,然后尝试使用我在浏览器控制台中测试并运行的JS执行,但是当我尝试通过硒webdriver,它不再起作用。

The JS function is: JS函数是:

webDriver.ExecuteScript("document.getElementById(\"container_form_containers_21_use\").click();");

The error I receive from this is " 我收到的错误是“

OpenQA.Selenium.WebDriverException: 'unknown error: Cannot read property 'click' of null" OpenQA.Selenium.WebDriverException:'未知错误:无法读取null的属性'click'

and the other normal selenium function I use for the click is: 我用于单击的其他普通硒功能是:

webDriver.FindElement(By.XPath("//*[@id='uniform container_form_containers_21_use']")).Click() ; webDriver.FindElement(By.XPath("//*[@id='uniform container_form_containers_21_use']")).Click() ;

The error I receive from this is 我收到的错误是

"OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='uniform - container_form_containers_21_use']"}" “ OpenQA.Selenium.NoSuchElementException:'没有这样的元素:无法找到元素:{” method“:” xpath“,” selector“:” // * [@ id ='uniform-container_form_containers_21_use']“}”

Note that 注意

document.getElementById(\"container_form_containers_21_use\").click();

works fine when I execute it in the browser console. 当我在浏览器控制台中执行它时,它可以正常工作。

如果我没记错的话,您不能单击该复选框,需要发送空格键。

webDriver.ExecuteScript("document.getElementById('container_form_containers_21_use\')").SendKeys(Keys.Space);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM