Home > Back-end >  Selenium: Trying to login but the same page is reloaded after click
Selenium: Trying to login but the same page is reloaded after click

Time:02-10

I am trying to login a page.

I entered the e-mail & password inputs by element.sendKeys() without any error.

After that I need to click the 'loginButton' button. But the button is defined as non keyboard-focusable.

When I run the automation, the button is clicked. But the automation is not continue with the next page (main page); just reloads the same page with empty inputs.

I tried several ways to click the button and also tried to enter by using 'ENTER' key;

// **1.**
 loginButton.click();

// **2.**
 robot.keyPress(KeyEvent.VK_ENTER);

// **3.**
 Actions act = new Actions(dDriver);
 act.moveToElement(driver.findElement(By.id("loginButton"))).click().build().perform();

// **4.**
 JavascriptExecutor executor;

All of them seems that I can click the button but after that as I mentioned, the page is reloaded, not continue with the next page.

What else can I try?

CodePudding user response:

A problem I can think about is that you are typing the information too fast and hit the login button right away.

Try to wait before clicking the Login button 0.3 seconds:

Thread.Sleep(300);

Or even a full second:

Thread.sleep(1000);

Tell me if this solves your issue.

Also, the site might be disabled for automation code so add these to your ChromeOptions:

ChromeOptions options = new ChromeOptions(); 
options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"}); 
WebDriver driver = new ChromeDriver(options); 

Or

options.addArguments("disable-infobars");

CodePudding user response:

To click() on the element Giriş Yap you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.green_flat#loginButton"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='green_flat' and @id='loginButton'][text()='Giriş Yap']"))).click();
    
  • Related