click command in selenium webdriver does not work
Asked Answered
M

10

53

I have just recently done an export of my selenium IDE code to selenium web driver. I have found that a lot of the commands that worked in IDE either fail to work or selenium web driver claims to not support at all. So far I've been tackling these issues one at a time which is less than ideal...

Currently I'm working on finding out why clicking on a button does not work with web driver while it had previously worked in selenium IDE. My browser is FF 13 and my OS is Ubuntu.

Code Snippet

WebElement loginButton = driver.findElement(By.name("submit"));
loginButton.click();

I had previously tried

driver.findElement(By.name("submit")).click();

however the above line failed as well. The element is getting selected, however it does not log us in as I would like. I found other pages with similar problems, but their problem seemed to be with Internet Explorer not Firefox. I don't even want to think about the problems IE will give me down the road.

thanks,

P.S. A tip on a better way to migrate from selenium IDE to Selenium Webdriver without losing all the tests I've written could solve this issue as well.

Morton answered 26/7, 2012 at 19:38 Comment(2)
did you try to assert loginButton.isDisplayed();?Harve
when you say the click() operation failed? Do you get any errors or exceptions.Please post them to understand why the click() operation fails.Sublingual
V
104

If you know for sure that the element is present, you could try this to simulate the click - if .Click() isn't working

driver.findElement(By.name("submit")).sendKeys(Keys.RETURN);

or

driver.findElement(By.name("submit")).sendKeys(Keys.ENTER);
Victualer answered 27/7, 2012 at 13:14 Comment(7)
@PavelJanicek either one will do the trick but edited to show both ;)Victualer
wow. I did not know this. Upvote for teaching me something new :)Hogweed
these commands worked for me. Though it turned out my problem was related to CAS authentication. Thanks for the answer!Morton
@Victualer Love it! Even does this for elements outside the bounds of the screen!Olid
@Victualer - Enter, Return, Space, JavaScript Executor, Actions - None of these work for me. It works if I put a small wait before my click. The element does not seem disabled. So, I dont know why a wait would be needed.Raft
Caution: it will also execute the "click" if there is a popup window open, eg. if an error occurred. For that reason, I can't use this solution.Implicit
No idea why but in my case I had to combine click() with sendkeys() alone by themselves they didn't work. So first came click() then sendkeys()Keratinize
M
10

A major thing to watch out for is whether a button is Enabled or not. You can still click them and nothing will fall over and the element is there but it is not ready to be clicked on so just doesnt do anything.

I've been using webdriver and its taken me most of the day to figure this out!

The following method seems to work reliably (in my environment for one button!)

    private void TryClick(By selector)
    {
        var wait = WaitUpTo(TimeSpan.FromSeconds(10));
        var element = wait.Until(ExpectedConditions.ElementIsVisible((selector)));

        //really important bit!
        WaitUpTo(TimeSpan.FromSeconds(5))
            .Until(d => element.Enabled);

        element.Click();
    }

you use it something like

TryClick(By.XPath("//button[contains(.//*,'Some Text')]"));
Meredeth answered 27/1, 2014 at 17:29 Comment(0)
S
8

WebElement.click() click is found to be not working if the page is zoomed in or out.

I had my page zoomed out to 85%.

If you reset the page zooming in browser using (ctrl + + and ctrl + - ) to 100%, clicks will start working.

Issue was found with chrome version 86.0.4240.111

Scorper answered 28/10, 2020 at 11:40 Comment(0)
H
4

Please refer here https://code.google.com/p/selenium/issues/detail?id=6756 In crux

Please open the system display settings and ensure that font size is set to 100% It worked surprisingly

Headley answered 24/12, 2013 at 9:10 Comment(0)
D
2

There's nothing wrong with either version of your code. Whatever is causing this, that's not it.

Have you triple checked your locator? Your element definitely has name=submit not id=submit?

Degradable answered 27/7, 2012 at 6:14 Comment(0)
M
2

Thanks for all the answers everyone! I have found a solution, turns out I didn't provide enough code in my question.

The problem was NOT with the click() function after all, but instead related to cas authentication used with my project. In Selenium IDE my login test executed a "open" command to the following location,

/cas/login?service=https%1F%8FAPPNAME%2FMOREURL%2Fj_spring_cas_security

That worked. I exported the test to Selenium webdriver which naturally preserved that location. The command in Selenium Webdriver was,

driver.get(baseUrl + "/cas/login?service=https%1A%2F%8FAPPNAME%2FMOREURL%2Fj_spring_cas_security");

For reasons I have yet to understand the above failed. When I changed it to,

driver.get(baseUrl + "MOREURL/");

The click command suddenly started to work... I will edit this answer if I can figure out why exactly this is.

Note: I obscured the URLs used above to protect my company's product.

Morton answered 27/7, 2012 at 15:52 Comment(2)
You said that it failed on IE not on firefox. Are you sure this situation only caused by location?Peruke
Please edit the original question instead of posting an update as an answer.Ioab
S
2

I was using firefox and some reason, it was not taking the click command though from past 2months it was working. My feeling was to make use of sendKeys and this page solved the problem. Now I am using sendKeys(Keys.Enter)

Spanjian answered 3/9, 2013 at 14:1 Comment(1)
This is a comment, not an answer. Why is this upvoted?Olid
T
2

This is a long standing issue with chromedriver(still present in 2020).

In Chrome I changed from a zoom of 90% to 100% and that solved the problem. ref

I find TheLifeOfSteve's answer more reliable.

Tersanctus answered 29/4, 2020 at 14:40 Comment(0)
P
2

I was working with EasyRepro, and when I debugged my code it was clicking on the element which is visible and enabled, and not navigating as expected. But finally I understood the root cause for the issue.

My Chrome was zoomed out 90%

Once i reset the zoom level, it clicked on the correct element and successfully navigated to next page.

Psychologize answered 21/5, 2020 at 19:47 Comment(0)
F
0

I use a function like below to make it work, or at least try a few times. Basically check the current_url until you get a new page.

def make_button_work(driver, path, max_try = 3):

strUrl_1 = driver.current_url
strUrl_2 = driver.current_url
try_num = 0
while strUrl_1 == strUrl_2:
    try_num +=1
    button = driver.find_element_by_xpath(path)
    time.sleep(1)
    button.click()
    time.sleep(1)
    strUrl_2 = driver.current_url
    if max_try == try_num:
        print('Reached max_try')
        break
Fizzle answered 6/4, 2022 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.