1

EDIT: I have cleaned this up a bit.

I have a button that looks like this:

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

Firebug:

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

I'm trying to click it and have tried pretty much everything for 2 days, webdriver does not find the element, IDE does find it:

//This was my first approach, it should work. It works in IDE, but not Webdriver:

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

//then perhaps this should do the trick, hint: It doesn't:

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

Ok, Id not working, I get it. Then this should work at least:

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

It feels like I am missing something obvious here, some help please?

Additional information:

I have added a 5 second wait, the page is completely loaded.

This button is located in a table: enter image description here

The Xpath is

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

Webdriver error, no matter what I throw at it, is: Unable to locate element

I have used both 'click' and 'submit', still no success.

Jon Carlstedt
  • 2,317
  • 5
  • 22
  • 27
  • Try `driver.findElement(By.id("applyRuleButton")).submit()`. If it doesn't work, then can you please specify the exception you get for `driver.findElement(By.id("applyRuleButton"))`? – barak manos Feb 03 '14 at 11:16
  • Try driver.findElement(By.id("applyRuleButton")).sendKeys(" "). I once had the same issue and this resolved the issue. I found that it was an issue with Webdriver. – Vikas Ojha Feb 03 '14 at 11:22
  • .submit and .sendKeys does not help. Unfortunately there seems to be an known issue with FF/selenium not showing exceptions. I'll see if I can do a rollback so I can see the exception. – Jon Carlstedt Feb 03 '14 at 12:07
  • Doesn't work means what? Does it find the element? Do you get any exceptions? Any different on another browser? Any different on another version of Firefox? – Arran Feb 03 '14 at 12:56
  • Sorry, It doesn't seem to find the element at all. No exceptions due to a webdriver/FF bug, I will try to roll back to an older version of FF. – Jon Carlstedt Feb 03 '14 at 13:20
  • please post bigger part of html. Perhaps there is a frame or something similar? – Erki M. Feb 12 '14 at 10:33

9 Answers9

4

I think in this case there are two possibilities :

  1. Either there is another element having same id/xpath.

  2. OR Element present in another iframe.

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
  • Thanks, Shubham from 2014. https://www.selenium.dev/documentation/webdriver/interactions/frames/ – Tho Mai Dec 03 '22 at 14:54
2

Is the button visible. Selenium click (latest firefox 26 and latest webdriver 2.39.0) does not sometimes implicitly scroll; Or it may not scroll it fully. So scroll it into view - Scroll Element into View with Selenium and then it should work. Note Selenium Best Practise try to use By.Id,By.CSSSelector and if nothing gets use By.Xpath in the order of priority. ( Use the FireFinder, FireBug plugin to test XPath or CSS)

Community
  • 1
  • 1
Alex Punnen
  • 5,287
  • 3
  • 59
  • 71
1

This might be a synchronization issue. Such issues can be solved using smart waits.

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

And that should work perfectly fine.

Rakesh Raut
  • 183
  • 3
  • 12
0

There is absolutely nothing wrong with your selector. I just don't think you're invoking the click correctly.

Try:

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

If this doesn't work, then you might have to invoke a WebDriverWait since you have this question marked as [ajax]

ddavison
  • 28,221
  • 15
  • 85
  • 110
  • Nope, doesn't find the button. – Jon Carlstedt Feb 06 '14 at 12:40
  • 1
    it doesn't *find* the button? ok, could you do me a favor... what we need to do is verify that there is no duplicate id's.. this could be causing it. If your site has jQuery on it, could you open up your chrome dev tools and type `$("#applyRuleButton")` What does it return? Just 1 element, or more? – ddavison Feb 06 '14 at 15:07
  • That just outputs: [] – Jon Carlstedt Feb 06 '14 at 15:14
  • 2
    that's your issue. you are not actually on the page. – ddavison Feb 06 '14 at 15:41
  • Could it be that the container holding your button is actually in an iFrame? Otherwise, if you call `document.getElementById("applyRuleButton")` it should find and element. According to your previous comment, `$("#applyRuleButton")` does not find any element, that means it's not on a page. – ssmiech Feb 08 '14 at 12:20
0

First try to identify the button by writting correct xpath using firebug, if you are able to identify button by that xpath then use that xpath while writing your script.

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

This is ajax application use proper explicit/ webdriver wait till the button gets downloaded

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
SATYA
  • 52
  • 3
  • I can find the object with firebug, the problem is that webdriver can't. It should be quite straight forward, it's a simple operation, but somehow webdriver really don't want to do it. – Jon Carlstedt Feb 06 '14 at 12:41
  • Check whether webelement is downloaded or not boolean flag=driver.findElement(By.xpath("//input[@ type='submit' and @ id='applyRuleButton'")).isDisplayed; if(flag){driver.findElement(By.xpath("//input[@ type='submit' and @ id='applyRuleButton'")).click(); } – SATYA Feb 08 '14 at 00:58
0

Try this:

driver.findElement(By.Name("filtersContainer:applyRuleButton"));

If this doesn't help, check whether this button is located in any other frame. If so, you may have to find and move your focus to that frame and then locate this button.

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Vysakh
  • 213
  • 2
  • 3
  • 10
0

Could you post the entire html?

As a simple experiment, I took the html snippet that you posted and wrote a short python script that invokes selenium:

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()

I can find the button and extract the attribute "name" which prints "filtersContainer:applyRuleButton". This is admittedly a very limited experiment, but it suggests that the issue is related to not being where you think you are on the page.

C R
  • 2,182
  • 5
  • 32
  • 41
0

I see that this thread is old, but I was looking at it today (Sept/2021) as I was having the same problem: I could see the name / id/ type of the button, but it would never be found. I discovered that when I had clicked in a prior link, it opened a new tab in my browser, but Selenium did not change the focus to the new tab, so it couldn't find the ID of the button I was looking for.

I solved it with :

driver.find_element_by_id("export").click() #driver
time.sleep(2) 

driver.switch_to.window(driver.window_handles[1]) # Change focus to the new tab
driver.find_element_by_id("0001btn").click() #click

driver.close() #close new tab
gurezende
  • 176
  • 9
0

switching to a specific frame helped me to resolve the same issue. (python + selenium)

I installed the Selenium Recorder extension on chrome and recorded my steps, and found out that the recorder had a step to select a frame = 0, so adding

  self.home_page.driver.switch_to.frame(0)
    self.home_page.click_on_element_by_id("clickSubmit")

solved the problem.

Iryna D.
  • 1
  • 1