javascript 硒无法找到元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27832266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium Unable to Find Element
提问by klib
I am trying to find an element on a website using Selenium. The page I am looking at is:
我正在尝试使用 Selenium 在网站上查找元素。我正在查看的页面是:
http://www.usaswimming.org/DesktopDefault.aspx?TabId=1470&Alias=Rainbow&Lang=en-US
http://www.usaswimming.org/DesktopDefault.aspx?TabId=1470&Alias=Rainbow&Lang=en-US
Specifically I am trying to find the element for the "Last Name" input box and send keys in Java. the html looks like this for the text box:
具体来说,我试图找到“姓氏”输入框的元素并在 Java 中发送密钥。文本框的 html 如下所示:
<div class="field">
<input id="ctl62_txtSearchLastName" type="text" maxlength="36" name="ctl00$ctl62$txtSearchLastName">
</div>
Therefore, I initially attempted to get the element through the id which is unique:
因此,我最初尝试通过唯一的 id 获取元素:
WebDriver driver = new InternetExplorerDriver();
driver.get(timeSearchSite);
...
driver.findElement(By.id("ctl62_txtSearchLastName")).sendKeys(lastName);
However this generated the following error:
但是,这产生了以下错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == ctl62_txtSearchLastName (WARNING: The server did not provide any stacktrace information)
线程“main” org.openqa.selenium.NoSuchElementException 中的异常:无法找到 id == ctl62_txtSearchLastName 的元素(警告:服务器未提供任何堆栈跟踪信息)
I also tried to use the name which failed similarly. Since this is a javascipt page I thought that I might need to wait before trying to access the element. I tried an explicit and an implicit wait and both resulted in timeouts. My next thought was that I was in the wrong frame but I cannot seem to find another frame. I tried indexing since I don't have names but I couldn't locate any other frames. There is this at the top of the page which might be messing selenium up:
我也尝试使用同样失败的名称。由于这是一个 javascipt 页面,我认为在尝试访问该元素之前可能需要等待。我尝试了显式和隐式等待,都导致超时。我的下一个想法是我在错误的框架中,但我似乎无法找到另一个框架。我尝试建立索引,因为我没有名字,但我找不到任何其他框架。页面顶部有一个可能会弄乱 selenium 的内容:
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-WMWM93" height="0" width="0"
style="display: none; visibility: hidden"></iframe></noscript>
What could be causing this error? How can I locate the element? Any help would be greatly appreciated/
什么可能导致此错误?如何定位元素?任何帮助将不胜感激/
回答by alecxe
Using an explicit waitworked for me (tried both chrome and firefox):
使用显式等待对我有用(尝试了 chrome 和 firefox):
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("ctl62_txtSearchLastName"))
);
element.sendKeys("test");
Resulted into:
结果变成: