1

I have a form that contains some input types 'text' which selenium finds and populates ok. However it cannot find the input type submit on form, the structure looks somelike this this:

<div>
<div>
<form>
      <div>other elements are here....</div>
         <p> <input class="btn btn-success" type="submit" name="yt0" value="Register Now" /></p>     
</form>
</div>
<div>   

I have tried a few methods the latest of which is:

driver.findelement(By.name("yt0")).click();

also tried:

driver.findelement(By.name("yt0")).submit();

I can see the element clearly on the page, and selenium has entered all the text needed for the elements above the submit.

Is it possible that Selenium cannot find the submit because its contained in a hierarchy of DIVs? Thanks

Paul Harris
  • 5,769
  • 1
  • 25
  • 41
spidee76
  • 71
  • 1
  • 1
  • 7

5 Answers5

2

Form is the one which has to be submitted.

Use

driver.findElement(By.tagName("form")).submit();

Hope it helps.

QAMate.com
  • 141
  • 1
  • 3
1

Selenium should be fine to find the above element, from the information you have provided I cannot see why it would not be able to click it. Have you tried a different locator?

One example would be:

driver.findElement(By.className("btn-success")).click();

or maybe you can look at others here

There are a few other things that could be happening is the button disabled when selenium clicks it or the button appears only once the form is filled in? If so you will need to add a fluent wait for it to become enabled (or use a thread.sleep(500) to test it works before spending the time on the wait).

Paul Harris
  • 5,769
  • 1
  • 25
  • 41
1

Your Code looks fine. But please check if the submit is hidden if the element is hidden we have to use Javascript to click on the hidden element. But before that please try using a different locator instead of name try xpath.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
user2719138
  • 5
  • 1
  • 1
  • 4
0

Another possibility is the element appear in DOM however it is not clickable yet. Perhaps your can try study and implement explicit wait.

Here are a good samples of WaitTools.java

Hope this helps :D

cL83
  • 489
  • 5
  • 17
0

Selenium often is faster than the page loading, say for example you clicked a link and it takes you another page. There is not always an implicit wait, maybe you need to wait for element to become visible.

This can be done with Explicit waits, see code below.

import import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

WebDriverWait wait = new WebDriverWait(driver, seconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By
        .className(className)));

Another option is to use JavaScriptExecutor, if the Input is hidden this can help use Javascript to change the attributes.

Remember that sometimes you need to wait for an element, and it is a good idea to have a helper class with static methods so you can easily call these methods, without rewriting the code, here is a sample method.

public static void waitForVisibilityByClass(WebDriver driver,
            String className, int seconds) {
        WebDriverWait wait = new WebDriverWait(driver, seconds);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By
                .className(className)));
    }

Let me know if this doesn't help.

broot02
  • 103
  • 1
  • 2
  • 11