'Not able to click on Login button using selenium webdriver and Java

Hi I am trying to automate https://www.nextgenerationautomation.com and unable to click on login / SignUp button using Selenium 4

Steps:

  1. Navigate to URL: https://www.nextgenerationautomation.com
  2. Click on LogIn/SignUp button.

Issue: when I am using Thread.Sleep, code is working fine but when I am using explicit wait & implicit wait it's not working.
I have added Implicit in my base class at the time of driver initialization.

Here is the code that I have tried.

public class nextGenAutomationLoginPage extends Base {

    @FindBy(xpath = "(//button[@class='_1YW_5'])[1]")
    WebElement login;

    public nextGenAutomationLoginPage() {
        super();
        PageFactory.initElements(driver, this);
        // TODO Auto-generated constructor stub
    }

    public void clickOnLogin() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        System.out.println(driver.getTitle());
        // Thread.sleep(2000);
        wait.until(ExpectedConditions.elementToBeClickable(login));
        //wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(login, By.xpath("//div[@class='_10b1I']") ));
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
        js.executeScript("arguments[0].scrollIntoView();", login);
        login.click();
        //driver.findElement(By.xpath("(//button[@class='_1YW_5'])[1]")).click();
        wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@id='comp-kaoxkn4a1']")));
        String sign = driver.findElement(By.xpath("//div[@id='comp-kaoxkn4a1']/h1/span")).getText();
        System.out.println(sign);
    }

Note: I tried to add Scroll as well to add some wait before clicking.

DOM: enter image description here Please let me know if i am missing anything here.



Solution 1:[1]

Hi I got the point whats happening here :

  1. Manually navigate to URL: https://www.nextgenerationautomation.com (Selenium also loading this URL)
  2. Manually Immediately keep clicking on "LogIn/SignUp button" (Selenium also able to click therefore NO error in console )
  3. "LogIn/SignUp" page not opening unless enter image description here (LinkedIn following count widget ) gets appear on screen
  4. Once enter image description here gets appear manually click on "LogIn/SignUp button", now Login page is opening (Selenium able to click after Thread.Sleep)

Summery:

  1. This is a codding defect on that page.
  2. "LogIn/SignUp" is not clickable until enter image description here gets added on page
  3. Your selenium code is perfectly fine :)
  4. Xpath I have used is //span[text()='Log In / SignUp']

Thanks KS

Solution 2:[2]

To click() on the element with text as Log In / SignUp you can use either of the following Locator Strategies:

  • xpath:

    driver.findElement(By.xpath("//div[starts-with(@id, 'defaultAvatar')]//following::span[text()='Log In / SignUp']")).click();
    

However, as the element is a dynamic element, so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[starts-with(@id, 'defaultAvatar')]//following::span[text()='Log In / SignUp']"))).click();
    

Solution 3:[3]

Actually your code is clicking the login button before the page is loaded properly. please add visibility condition.

wait.until(ExpectedConditions.visibilityOfElementLocated(by));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sudeep Bidwai
Solution 2
Solution 3 Jayanth Bala