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
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();
}
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With