0

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.

3 Answers 3

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

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

Comments

0

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

6 Comments

I tried this code but still not working ``` public class nextGenAutomationLoginPage extends Base { @FindBy(xpath = "//span[text()='Log In / SignUp']") WebElement login; public void clickOnLogin() throws InterruptedException { WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Log In / SignUp']"))).click();
When i am using Thread.Sleep , its working. Can you please help me to get rid of Thread.Sleep somwhow
What error do you see exactly?
No error its just not clicking on Login .. so the assertions for next page is failing
Can you try the updated xpath and let me know the status please?
|
0

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

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

3 Comments

Tried , its not working and surprisingly its not throwing any error ..:-(
Your code is clicking the element properly. That is why, no error.i ll try the same and let you know
I am seeing this console error, it is clicking the button properly. The resource <url> was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate as value and it is preloaded intentionally.

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.