3

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

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your reply! I copied your code and only change "web Driver" to "driver" because that is what I name it and I got this: imgur.com/DRocb1r
We are referencing different List classes. You need to tell your code where to find the proper class. Add this line to the top of your class file: import java.util.List; Or, you can change the failing line of code to reference java.util.List instead of List.
Thank you! Worked like a charm. Do you mind explaining the code though?
I added an explanation in case you are curious, but check out Greg's answer for a more concise solution.
2

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();

Comments

0

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();

Comments

0

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.