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.

Jyoti
  • 35
  • 9

3 Answers3

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

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();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    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(); – Jyoti Nov 07 '21 at 08:05
  • When i am using Thread.Sleep , its working. Can you please help me to get rid of Thread.Sleep somwhow – Jyoti Nov 07 '21 at 08:08
  • What error do you see exactly? – undetected Selenium Nov 07 '21 at 10:40
  • No error its just not clicking on Login .. so the assertions for next page is failing – Jyoti Nov 07 '21 at 10:59
  • Can you try the updated xpath and let me know the status please? – undetected Selenium Nov 07 '21 at 11:08
  • Its still not working.... However the updtated xpath worked with Thread.Sleep – Jyoti Nov 07 '21 at 11:23
0

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

wait.until(ExpectedConditions.visibilityOfElementLocated(by));
Jayanth Bala
  • 758
  • 1
  • 5
  • 11
  • Tried , its not working and surprisingly its not throwing any error ..:-( – Jyoti Nov 08 '21 at 11:32
  • Your code is clicking the element properly. That is why, no error.i ll try the same and let you know – Jayanth Bala Nov 09 '21 at 02:02
  • I am seeing this console error, it is clicking the button properly. The resource 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. – Jayanth Bala Nov 09 '21 at 04:52