Selenium webdriver找不到按钮

时间:2014-02-03 11:07:56

标签: ajax firefox selenium

编辑: 我已经清理了一下。

我有一个看起来像这样的按钮:

<input id="applyRuleButton" class="Button" name="filtersContainer:applyRuleButton"
 value="Apply" onclick="wicketShow('applyRuleButton--ajax-indicator');var
 wcall=wicketSubmitFormById('id256', '?wicket:interface=:23:form:filtersContainer:applyRuleButton:
:IActivePageBehaviorListener:0:&amp;wicket:ignoreIfNotActive=true',
 'filtersContainer:applyRuleButton' ,function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this),function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this), function() {return
 Wicket.$$(this)&amp;&amp;Wicket.$$('id256')}.bind(this));;; return false;" type="submit">

萤火虫:

    <input id="applyRuleButton" class="Button" type="submit" 
onclick="wicketShow('applyRuleButton--ajax-indicator');var 
wcall=wicketSubmitFormById('id2ee', 
'?wicket:interface=:29:form:filtersContainer:applyRuleButton::IActivePageBehaviorListener:0
:&wicket:ignoreIfNotActive=true', 'filtersContainer:applyRuleButton' ,function() { 
;wicketHide('applyRuleButton--ajax-indicator');}.bind(this),function() { 
;wicketHide('applyRuleButton--ajax-indicator');}.bind(this), function() {return 
Wicket.$$(this)&&Wicket.$$('id2ee')}.bind(this));;; return false;" value="Apply" 
name="filtersContainer:applyRuleButton">

我正在尝试点击它并且已经尝试了两天,webdriver没有找到该元素,IDE确实找到了它:

//这是我的第一个方法,应该可行。 它适用于IDE,但不适用于Webdriver:

driver.findElement(By.id("applyRuleButton")).click();

//那么也许这应该可以解决问题,提示:它没有:

WebElement element3 = driver.findElement(By.id("applyRuleButton"));
JavascriptExecutor executor3 = (JavascriptExecutor)driver;
executor3.executeScript("arguments[0].click();", element3);

好的,我没有工作,我明白了。 那么这至少应该起作用:

driver.findElement(By.xpath("//table/tbody/tr/td/div/div/table/tbody/tr[6]/td/input[@id='applyRuleButton']")).click();

感觉我在这里遗漏了一些明显的东西,请帮忙吗?

其他信息:

我已经添加了5秒等待,页面已完全加载。

此按钮位于表格中: enter image description here

Xpath是

/html/body/div[4]/div[2]/form/div[3]/div/div/table/tbody/tr/td/div/div/table/tbody/tr[6]/td/input

Webdriver错误,无论我抛出什么,都是:无法找到元素

我使用了'click'和'submit',仍然没有成功。

7 个答案:

答案 0 :(得分:3)

我认为在这种情况下有两种可能性:

1)有另一个元素具有相同的id / xpath。

2)OR元素存在于另一个iframe中。

答案 1 :(得分:2)

按钮是否可见。 Selenium click(最新的firefox 26和最新的webdriver 2.39.0)有时不会隐式滚动;或者它可能无法完全滚动它。所以将其滚动到视图 - Scroll Element into View with Selenium然后它应该工作。 注意Selenium Best Practice尝试使用By.Id,By.CSSSelector,如果没有按优先级顺序使用By.Xpath。 (使用FireFinder,FireBug插件测试XPath或CSS)

答案 2 :(得分:1)

这可能是同步问题。这些问题可以通过智能等待来解决。

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists((By.Id("applyRuleButton"))));      
WebElement element3 = driver.findElement(By.id("applyRuleButton"));

这应该可以很好地完成。

答案 3 :(得分:0)

您的选择器绝对没有问题。我只是觉得你没有正确地调用点击。

尝试:

driver.findElement(By.id("applyRuleButton")).click();

如果这不起作用,那么您可能必须调用WebDriverWait,因为您将此问题标记为[ajax]

答案 4 :(得分:0)

首先尝试通过使用firebug写入正确的xpath来识别按钮,如果你能够通过该xpath识别按钮,那么在编写脚本时使用该xpath。

  driver.findElement(By.xpath("//input[@ type='submit' and @ id='applyRuleButton'")).click();  

这是ajax应用程序使用正确的显式/ webdriver等待按钮下载

答案 5 :(得分:0)

driver.findElement(By.Name(“filtersContainer:applyRuleButton”))怎么样;);你试过这个吗?

如果这没有帮助,请检查此按钮是否位于任何其他框架中。如果是这样,您可能必须找到并将焦点移动到该框架,然后找到此按钮..

答案 6 :(得分:0)

你可以发布整个HTML吗?

作为一个简单的实验,我拿了你发布的html片段并编写了一个调用selenium的短python脚本:

from selenium import webdriver
br = webdriver.Firefox()
br.get("put your path to snippet here")
button = br.find_element_by_id("applyRuleButton")
print button.get_attribute("name")
button.click()
br.close()

我可以找到按钮并提取打印“filtersContainer:applyRuleButton”的属性“name”。这无疑是一个非常有限的实验,但它表明问题与你认为自己在页面上的位置无关。