Selenium Error when using JavaScript or getting elements

你离开我真会死。 提交于 2019-12-13 05:52:59

问题


Using Seleneium 2.25, I've had a lot of issues arise.

I'm trying to use Selenium Remote Driver on a remote machine (Server) from my computer (local / client). However, when I try to use DesiresCapabilities.Htmlunit() It will locate the elements, but it says they are not visible. I'm completely stumped by this. I'm not sure why it can be found but then not visible.

So then I tried to use some JavaScript in order force it. It comes back and throws an error saying that the webpage can not execute javascript before the page is loaded. How is this possible when I did an implicate wait, and it found the element it was waiting for?

DesiredCapabilities iecapa = DesiredCapabilities.HtmlUnit();
iecapa.IsJavaScriptEnabled = true;
driver = new RemoteWebDriver(new Uri("http://<IP of server>:4444/wd/hub"), iecapa);

IJavaScriptExecutor jQuery = ((IJavaScriptExecutor)(driver));
addressElement = (IWebElement)jQuery.ExecuteScript("return document.GetElementByName('searchAddress')");

So if anyone would like to help me, it would be greatly appreciated! Thank you!

http://imageshack.us/photo/my-images/163/seleniumhtmluniterror.jpg/

that is the error. StackOverflow wont let me post it here. =(


回答1:


Try this:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 0, 30));
wait.Until(p => driver.FindElement(By.Name("searchAddress")));

IJavaScriptExecutor jQuery = ((IJavaScriptExecutor)(driver));
addressElement = (IWebElement)jQuery.ExecuteScript("return document.GetElementByName('searchAddress')");

Also you can add check in JavaScript:

if (document.readyState.toLowerCase()=="complete") 
return document.GetElementByName('searchAddress');
return 'Error';


来源:https://stackoverflow.com/questions/11688694/selenium-error-when-using-javascript-or-getting-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!