Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click command in selenium webdriver does not work

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.

like image 521
OrwellHindenberg Avatar asked Jul 26 '12 19:07

OrwellHindenberg


People also ask

Why click is not working in Selenium?

We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.

How does click operation work in Selenium?

We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.

Why are elements not clickable?

The exception “Element is not clickable at point” might be thrown when the element is not under focus or the action is being performed on the incorrect WebElement. In such cases, you have to switch to the actual element and perform the click action.

What is alternative for click in Selenium?

Alternative of click() in Selenium We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method. The parameters – arguments[0]. click() and locator of the element on which the click is to be performed are passed to this method.


2 Answers

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); 
like image 140
TheLifeOfSteve Avatar answered Oct 05 '22 22:10

TheLifeOfSteve


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')]")); 
like image 42
JonnyRaa Avatar answered Oct 05 '22 22:10

JonnyRaa