Selenium无法找到元素 [英] Selenium Unable to Find Element

查看:185
本文介绍了Selenium无法找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Selenium在网站上找到一个元素。我正在查看的页面是:

I am trying to find an element on a website using Selenium. The page I am looking at is:

http://www.usaswimming.org/DesktopDefault.aspx?TabId=1470&Alias=Rainbow&Lang=en-US

具体来说,我试图找到姓氏输入框的元素并用Java发送密钥。对于文本框,html看起来像这样:

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:

<div class="field">
<input id="ctl62_txtSearchLastName" type="text" maxlength="36" name="ctl00$ctl62$txtSearchLastName">
</div>

因此,我最初尝试通过唯一的id获取元素:

Therefore, I initially attempted to get the element through the id which is unique:

WebDriver driver = new InternetExplorerDriver();
driver.get(timeSearchSite);
...
driver.findElement(By.id("ctl62_txtSearchLastName")).sendKeys(lastName);

然而,这会产生以下错误:

However this generated the following error:

线程main中的异常org.openqa.selenium.NoSuchElementException:无法找到id == ctl62_txtSearchLastName的元素(警告:服务器未提供任何堆栈跟踪信息)

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)

我也尝试使用类似失败的名称。由于这是一个javascipt页面,我认为在尝试访问该元素之前我可能需要等待。我尝试了一个明确的和隐含的等待,两者都导致了超时。我的下一个想法是,我在错误的框架,但我似乎无法找到另一个框架。我尝试索引,因为我没有名字,但我找不到任何其他帧。在页面顶部有这可能会弄乱selenium:

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:

<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/

推荐答案

使用 显式等待 为我工作(试过chrome和firefox):

Using an explicit wait worked for me (tried both chrome and firefox):

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.id("ctl62_txtSearchLastName"))
);

element.sendKeys("test");

导致:

这篇关于Selenium无法找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆