unno
unno

Reputation: 57

How to make selenium click a button with no id?

I am trying to click a button with no id in inspect element that is what I get when I search for the button in page source: <input type="submit" value="Log In" onclick="this.disabled=true;this.form.submit();">

It is the log in button in this link: https://myportal.lau.edu.lb/Pages/studentPortal.aspx

any help would be appreciated. I am using Java on Mac OS

Upvotes: 3

Views: 4665

Answers (4)

Dmitri T
Dmitri T

Reputation: 168157

The button has value attribute which uniquely identifies it, you can match the button using its value attribute with an XPath expression like:

//input[@value='Log In']

enter image description here

References:

Upvotes: 0

Spencer Melo
Spencer Melo

Reputation: 410

If you want to do it with css and avoid annoying issues (like page still loading and the button is not available yet), you can do the following:

By cssLocator = By.cssSelector("input[type='submit']");
int timeout = 5000;

//wait for element to be visible, even if the page is not fully loaded ( element not in the DOM ), after the timeout it will throw an TimeoutException.
new WebDriverWait(webDriver, timeout)
        .ignoring(NoSuchElementException.class) //This is when you know that the element may not be on the screen during the loading, if you try to wait for it when it's not on the DOM, it will throw this exception.
        .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(cssLocator));

WebElement loginBtn = driver.findElement(cssLocator));
loginBtn.click();

Upvotes: 0

srk
srk

Reputation: 1901

You need to find some other way to identify the button. In this case, you can use the tag name ("input") and the text we see on the screen ("Log In").

Try this to get you started:

webDriver.navigate().to("https://myportal.lau.edu.lb/Pages/studentPortal.aspx");

try {
    // Wait to make sure the page has fully loaded for this example.
    // Probably want something more sophisticated for your real test.
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

// Find the button based on tag name and value attribute.
WebElement button = null;
List<WebElement> inputs = webDriver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
    if (input.getAttribute("value").equals("Log In")) {
        button = input;
        break;
    }
}

if (button == null) {
    System.err.println("Cannot find button!");
} else {
    System.out.println("Clicking button now!");
    button.click();
}

Explanation

It is helpful to view the source code for this site in your browser.

List<WebElement> inputs = webDriver.findElements(By.tagName("input"));

This line of code searches the page and finds all elements with the tag name "input." That matches several elements, including the login button, as well as the username and password fields. So we need to narrow it down further...

for (WebElement input : inputs) {

This line of code loops over each input that we found above. Inside the loop, we will look more closely at the element to try to identify the login button.

if (input.getAttribute("value").equals("Log In")) {

As you noted in your original question, the login button has a "value" attribute with the value of "Log In." The other input elements do not have this attribute value. So in this line of code, we look for the one input element such that the value attribute is "Log In." Once we find that element, we have identified the button, so we store it to be clicked later.

Upvotes: 2

Greg Burghardt
Greg Burghardt

Reputation: 18868

This is trivial using an XPath expression:

String xpath = "//input[@type = 'submit' and @value = 'Log In']";
WebElement button = driver.findElement(By.xpath(xpath));

button.click();

Upvotes: 2

Related Questions