【发布时间】:2014-10-25 17:30:49
【问题描述】:
我在尝试使用 Selenium Web 驱动程序查找 Web 元素时遇到问题。
我已经用两个页面完成了这个,我想要抓取的两个元素都在很多类、id 等中。
这是第一个元素,我没有遇到任何问题。
<input type="text" class="input-large" name="host" value="">
我用这个来抓的:
WebElement HtmlHost = driver.findElement(By.name("host"));
现在这是我的问题开始的地方。我现在正试图抓住这个:
<button type="submit" name="bootBtn" class="btn btn-inverse btn-large">Start hashing</button>
我尝试使用所有这些功能抓取,但没有一个能够找到该元素。
driver.findElement(By.partialLinkText("Start "));
driver.findElement(By.linkText("Start Hashing"));
driver.findElement(By.name("bootBtn"));
driver.findElement(By.cssSelector("button[type='submit']"));
你建议我做什么?
谢谢。
【问题讨论】:
-
这些看起来都应该工作,但试试
By.CssSelector("button[type='submit']")怎么样? -
不,那也行不通。还有什么建议吗?
-
依赖链接文本的方法将不起作用,因为您正在寻找
button,而通过链接文本搜索的方法仅搜索a元素。按名称搜索和按 css 选择器搜索应该可以工作。按钮是否仅在其他内容出现后才显示在页面上?另外,您收到的确切错误消息是什么? -
当您加载页面时,该按钮就在那里。我收到 NoSuchElementException 错误。
-
是一个框架内的元素吗?