Automation
WebElement not found when Selenium script executes

4

Asked 2 years ago,
Active 2 years ago,
1 comment
•  356 views
DevRun asked this question 2 years ago
DevRun
2 years ago

I have been experimenting with some simple Automation scripts using Java and Selenium, but with this login script I am running into an issue where the web elements are not being found by my automation script. The code below is my current login automation script, and for obvious reasons the website, username, and password are removed from the code...

Note: I am not too familiar with using Selenium and Java so any advice would go a long way...

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Login {

  public static void main(String[] args) throws InterruptedException {
  
    // TODO Auto-generated method stub
  
    Login obj = new Login(); 
    obj.LoginScriptTest();
  
  }
  
  public void LoginScriptTest() throws InterruptedException {
    
    String curDir = System.getProperty("user.dir");
    System.setProperty("webdriver.chrome.driver", curDir+"\\drivers\\chromedriverexe.exe");
    ChromeOptions options = new ChromeOptions(); 
    options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"}); 
    WebDriver driver = new ChromeDriver(options); 
    driver.get("https://********.com");
    driver.findElement(By.xpath("//span[@class='myTestSpan']")).click(); 
    driver.findElement(By.xpath("//div[@class='myTestDiv']")).click(); 
    
      WebDriverWait wait3 = new WebDriverWait(driver, 4); 
      wait3.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//form[@method='post']"))); 
      WebElement userName = driver.findElement(By.id("username"));
      WebElement password = driver.findElement(By.id("password"));
  
      String username = "********@yahoo.com";  
      String pwd = "*******"; 
    
      for (int i = 0; i < username.length(); i++) {
    
        char c = username.charAt(i); 
        String s = new StringBuilder().append(c).toString();
        userName.sendKeys(s);
    
      }
    
     
      for (int i = 0; i < pwd.length(); i++) {
    
        char c = pwd.charAt(i); 
        String s = new StringBuilder().append(c).toString();
        password.sendKeys(s);
    
      }
    
         WebElement ele = driver.findElement(By.id("login")); 
         ele.sendKeys(Keys.ENTER);
    
    }
  
}
MazeAnyways commented 2 years ago
DevRun marked this answer 2 years ago
MazeAnyways 2 years ago

Not knowing too much about the website/application you are trying to automate there could be a couple reasons why Selenium is failing to locate the elements on the page, but in this case I think that the script is trying to execute before the page has fully rendered.

What is happening is that when your script is executing, it is looking to locate those elements on the page before they are actually visible to the automation script. This would then throw an "Element Not Visible" exception in your console, or something along these lines. To further debug whether this is the actual issue, you can make use of waits which I see you are already using. Try either of these.

Thread.sleep(2000);

The sleep command will tell the WebDriver to "stop" for x amount of time, in case of the code above "2000" is 2 seconds. Now this isn't the best "fix", but may point you in the right direction.

The approach I would prefer to take would be to use WebDriverWait, which you are already using. By making use of "ExpectedCondtions", you'll wait until the element is visible --or timeout has occurred.

WebDriverWait wait = new WebDriverWait(driver, x); 

Some more documentation on Waits can be found in the docs here, https://www.selenium.dev/documentation/webdriver/waits/

--hope this helps

2

1
relpy
answer
Leave a comment
Leave a comment