-1

I want to test a javaEE application server and I want to test it with Selenium, when Selenium connect he have to go through a login page, but the form of this login page is hidden so I'm trying to type text in a form that the hidden property with Selenium.

<form hidden name="formLogin" method="POST" action="/app/">
        <input type="hidden" name="home" value="true">
</form>
<form hidden name="formLogout" method="POST" action="/app/">
        <input type="hidden" name="home" value="false">
</form>

After reading this post, post on how to force selenium to locate the hidden field, I thought I had the solution, but it starts to drive me crazy.

I have the following code :

public class SeleniumTestKheops {
static WebDriver driver;
static Wait<WebDriver> wait;

public static void main(String[] args) throws InterruptedException {
    driver = new FirefoxDriver();
    wait= new WebDriverWait(driver, 30);
    final String url = "https://localhost:8444/app/";
    try {
    driver.navigate().to(url);

    JavascriptExecutor js = (JavascriptExecutor) driver;

    js.executeScript("document.getElementsByName('formLogin')[0].checked = true;");
    js.executeScript("document.getElementsByName('formLogout')[0].checked = true;");
    driver.findElement(By.xpath("//input")).clear();
   driver.findElement(By.xpath("//input")).sendKeys("root");
   driver.findElement(By.xpath("//div[2]/div/div/input")).clear();
   driver.findElement(By.xpath("//div[2]/div/div/input")).sendKeys("pass");

   driver.findElement(By.xpath("//a[contains(@href, '#')]")).click();
    } finally {
        //driver.close();
    }
}

}

But when I run it multiple time, the result is different, sometimes it works, and most of the times I have :

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

I don't understand the meaning of this, I'm sure, why is this randomly working? What am I doing wrong?

EDIT 1: I edited the code, I'm sure of it. When I spam the running of this code it works 1 on 5 times. I still don't know why. I'm looking for a way to check all the webelement that are visible on the page.

Community
  • 1
  • 1
Omegaspard
  • 1,828
  • 2
  • 24
  • 52

1 Answers1

0

It looks like your xpath selectors are too general.

You can wait for an element to be visible with

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//valid/xpath"))));

I'm confused what you're trying to do in your method. Your java code doesn't match your provided html. Please provide a more complete html example.

Using your current java code, you could wait for all elements like this:

// define reusable Bys
By usernameInput = By.xpath("//input");
By passwordInput = By.xpath("//div[2]/div/div/input");
By submitButton = By.xpath("//a[contains(@href, '#')]");

// wait for all elements to be visible
wait.until(ExpectedConditions.visibilityOfElementLocated(usernameInput));
wait.until(ExpectedConditions.visibilityOfElementLocated(passwordInput));
wait.until(ExpectedConditions.visibilityOfElementLocated(submitButton));
// clear username field and type username
driver.findElement(usernameInput).clear();
driver.findElement(usernameInput).sendKeys("root");
// clear password field and type password
driver.findElement(passwordInput).clear();
driver.findElement(passwordInput).sendKeys("pass");
// submit the form
driver.findElement(submitButton).click();
user2272115
  • 986
  • 1
  • 10
  • 22
  • Well i don't know why but i managed to make it workable by testning the 3 element with the isEnabled method. But it seems your way is cleaner. Thank you.So the exception was raised because the code continue his execution before the WebElement was visible ? – Omegaspard May 24 '16 at 15:19
  • @BobReynolds Adding a few lines of code to perform the isEnabled check probably gives the page a few milliseconds to load the elements. Yes, ElementNotVisibleException means that the element you tried to interact with is not visible in the firefox instance. – user2272115 May 24 '16 at 15:22