Selenium webdriver can't find button - ajax

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:&wicket:ignoreIfNotActive=true',
'filtersContainer:applyRuleButton' ,function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this),function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this), function() {return
Wicket.$$(this)&&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:
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.

I think in this case there are two possibilities :
Either there is another element having same id/xpath.
OR Element present in another iframe.

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)

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.

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]

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.

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.

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

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

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.

Related

Need help on clicking an element (Element is not clickable at point (62, 459)) - Capybara Ruby Selenium Automation

I am having below error message in my console while trying to click on a button element:
unknown error: Element is not clickable at point (62, 459).
Other element would receive the click: <i class="foo foo-chase-lemon font-size-13"></i>
Here's my code below:
#object = Page.new
#object.wait_until_btn_element_visible
#object.btn_element.click
I have tried with retry 5 times to click on it using rescue but didn't help.
Below code also didn't work where i tried to move to that element before click.
Capybara.page.driver.move_to.(#object.btn_element).perform
Any solution will be greatly appreciated.
I tried increasing the resolution/ scrolling the window. None of them worked on this specific scenario. These solution might work for others.
However, I resolved the issue by clicking the button using javascript "execute_script" method in my automation script.

Selenium Web Driver(Firefox) fails sometimes to find an element by id which is present in the web page

I am using Selenium Web Driver for Fire Fox to automate a web page inPython.
The issue is that sometime for some elements i am getting NoSuchElementException even though the element is present with the id i am searching for.
I am using find_element_by_id(id) method.
My code is
d = webdriver.Firefox()
elm5 = d.find_element_by_id("ctl00_bodyContent_tabGroupAdmin")
elm5.click()
The HTML for that element is:
<div id="ctl00_bodyContent_tabGroupAdmin" class="tab">
<div onclick="OpenTab(3); DisableProgressBarDisplay(); return false;">
Group Administrators</div>
<div class="cap_right">
</div>
</div>
Has anyone faced the same issue? Please suggest some work around.
Thanks
I would suggest that you have to wait for the element to appear.
have a look at this answer about how to wait
We had this problem, too, sometimes while using selenium. The page was too slow.
I don't know anything about Python, however i am working with selenium Webdriver using java, and i guess the issue lies bcz may you are not switched to the current frame.
You can check this by checking your html stuff by just going up and you will be able to find the current frame.
In java we used to do this as
driver.switchTo().frame(driver.findElement(By.name("officePane")));
you can check this similar in the python Hope it will help you.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
add these to the beginning of your code and
WebDriverWait(d, 20).until(EC.presence_of_element_located((By.ID, "ctl00_bodyContent_tabGroupAdmin")))
BTW, you can find and click elements in a single line as:
d.find_element_by_id("ctl00_bodyContent_tabGroupAdmin").click()
good luck and thanks for User to teach this solution for us.
Hope your problem is fixed now. But i was facing the same problem since last 2 weeks.
Here is the below code for your problem.
It will detect the frame and locate the element specially when the element is frequently detected and some times not detected.
WebDriver d = new FirefoxDriver(); <-- Intialize your Firefox driver
WebDriverWait frame = new WebDriverWait(driver, 50); <-- Creating wait object for frame
frame.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.tagName("iframe")));
WebDriverWait wait1 = new WebDriverWait(driver, 50); <-- Creating wait object for element
wait1.until(ExpectedConditions.elementToBeClickable(find_element_by_id("ctl00_bodyContent_tabGroupAdmin"))).click();
Hope this will solve your problem if its unsolved.

Selenium WebDriver - Unable to close select drop down menu in Chrome on Mac OS X

I have been Working with Selenium WebDriver for a few months now and I have a problem with a drop down menu within a web app that I am working on.
What is happening is that the test is opening the page, verifying several elements on the page by finding them and then ensuring they are displayed.
After doing that there is some text entered into different fields, then the option select box is clicked on to open the drop down menu.
Following this the test iterates through all the options in the drop down menu until it finds the one it needs, then clicks on that option.
At this point the option is selected but the drop down menu is not closed.
I have tried clicking on the option select again but this has no effect, during the rest of the test other pages are navigated to and the menu does not close.
Then the page is saved and then navigated away from.
However the drop down menu remains until the browser is closed.
This is the code from the app:
<select id="options" name="options" class="options">
<option value="option1 (auto)">option1 (auto)</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
</select>
the first solution I would try is to click on menu options in different ways. Selenium API provides us with this possibility.
1) locate e.g. css selectors of the elements.
String cssOption1 = "select[id='options']>option[value='option1 (auto)']";
String cssOption2 = "select[id='options']>option[value='option2']";
String cssOption3 = "select[id='options']>option[value='option3']";
Also don't forget to verify that you found elements properly e.g .in firepath, firebug addon in ffox:
approach 1
driver.findElement(By.cssSelector(cssOption2)).click();
approach 2 using actions builder API
WebElement mnuOptionElement;
mnuOptionElement = driver.findElement(By.cssSelector(cssOption2));
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnuOptionElement).click();
more info about Actions builder you can get here
approach 3 using jsExecutor to click on web element. Always works for me in all situations.
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssOption2+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
Hope this works for you
I have solved the problem with a work around, as this is the only way that I have found to work.
Firstly thank you eugene.polschikov for your answer although it didn't solve the problem it did open my eye somewhat, I had no knowledge of action builder, and it has given me some great ideas about future tests.
Also thank you to anyone who read this and pondered over a possible solution.
The workaround that is now in place is that the select is not opened.
The way the code works is that it would open the list and find the one it wanted and click on it, at this point the select wouldn't close, so now the code no longer opens the select in the first place, it clicks on the hidden option to select it, not 100% what i wanted, but it works.
Happy Programming,
Ben.
If a human can press Escape to exit the combobox, you can do that in Selenium by switching to the active element:
from selenium.webdriver.common.keys import Keys
element = driver.switch_to.active_element
element.send_keys(Keys.ESCAPE)

Element is not clickable at point (617, 690.5). Other element would receive the click: <th>...</th> (Selenium::WebDriver::Error::UnknownError)

I get the error above when trying to press the "add" button on the integration test I am trying to implement. I thought that was a delay with the javascript, and I tried the following, but none of them worked:
Then /^I should click "([^"]*)"$/ do |selector|
# if page.should have_selector(selector)
# find(selector).click
# end
# page.execute_script("$(#{selector}).click()")
find(selector).click
end
I tried all of the above, but that didn't solve the problem.
This is hardly a great answer, but I'll give it anyway...
I've only ever seen this problem when testing with Chrome. There are two solutions that worked for me:
Use Firefox instead of Chrome
Instead of find(selector).click, use page.execute_script("$(#{selector}).click()")
But since you've tried the second option and it didn't work, try using Firefox instead. Sorry :(
I found resizing the browser window made a similar problem go away:
Given(/^I open a new browser window$/) do
Capybara.current_session.current_window.resize_to(1000, 1000)
end
I was getting the same error in Capybara when trying to click_button a 'Save' button at the end of a page. Scrolling and bringing the element to view using JS proved to be an effective workaround.
def scroll_to(element)
script = <<-JS
arguments[0].scrollIntoView(true);
JS
Capybara.current_session.driver.browser.execute_script(script, element.native)
element # return the element
end
scroll_to(
find(:xpath, '//button[text()=\'Save\']')
).click
Update
For me, it turned out to be a mismatch between the version of installed Chrome and Chromedriver and no code change was necessary after all.
Chrome 61+ includes modifications to scrolling behaviors. A quick examination of Chromedriver page revealed that I had to upgrade my Chromedriver (from 2.29 to 2.32) in order to make the driver compatible with those changes. This was part of the Chromedriver 2.30 (Supports Chrome v59-61) release notes:
Fixes a bug where Chromedriver fails to click due to page scrolling changes in Chrome 61+.
I had a sticky header that was preventing Selenium from sending the click to the right radio button on both Chrome and Firefox (though Poltergeist worked fine). This is a hack to delete all fixed elements (like sticky headers), and it worked for me:
def kill_sticky_headers
# https://alisdair.mcdiarmid.org/kill-sticky-headers/
script = <<-EOS
(function () {
var i, elements = document.querySelectorAll('body *');
for (i = 0; i < elements.length; i++) {
if (getComputedStyle(elements[i]).position === 'fixed') {
elements[i].parentNode.removeChild(elements[i]);
}
}
})();
EOS
page.execute_script script
end
I was facing a similar problem where I have to check two check boxes one after the other. But I was getting the same above error, hence I added a wait in between my steps for checking the checkboxes. It's working fine and great.
Here are the steps:-
When I visit /administrator/user_profiles
And I press xpath link "//*[#id='1']"
Then I should see "Please wait for a moment..."
When I wait for 5 seconds
And I press xpath link "//*[#id='2']"
Then I should see "Please wait for a moment..."
When I visit /administrator/user_profiles_updates

Firefox WebDriver does not find the element of the page

I would like to use WebDriver (Firefox) to test my webpage, but I always get this message:
Unable to locate element: {"method":"xpath","selector":"//li[#id='something_spicy']"}
But, the really strange thing is that if I copy the element locator "//li[#id='something_spicy']" and paste it into Firefinder then it works. Basically, I ran out of thoughts about what could be the root cause.
I use C# and here is the code:
movedElement = driver.FindElement(By.XPath("//li[#id='" + originalOrderOfSportContainers[1] + "']"));
Did I do something wrong? Have I missed something?
provide a snippet of the html code of your page.
There is a chance that element is not there immediately after the page is loaded,
but appears a bit later as a result of some javascript function execution.
If this is the case, use explicit wait to wait for element to appear before using it.

Resources