简体   繁体   中英

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.

The JS function is:

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"

and the other normal selenium function I use for the click is:

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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