Compilation stuck at find element by xpath without giving any error



  • I'm trying to automate a project using selenium page object model.

    Below is the html code

       

    Prepaid Balance

    Rs. 67.58

    Valid till 29 Apr 2023

    Special Money

    Rs. 0.00

    Valid till 01 Oct 2022

    /reload-and-pay?number=768433670&redirect_uri=https%3A//qa.dialog.lk/selfcare

    This is my page class

    public class DataDetailViewPage extends TestBase {
    

    //@FindBy(xpath = "//span[text()='DATA']")
    WebElement dataTab;

    //@FindBy(xpath = "//*[@id="full-width-tabpanel-1"]/div/div/div[1]/div/div[1]")
    WebElement dataCard;

    //@FindBy(xpath = "/html/body/div[3]/div[3]")
    WebElement detailsPopup;

    //@FindBy(xpath = "//*[@id="full-width-tabpanel-any"]/div/div[1]/div[3]/div/div/div")
    WebElement detailCard;

    //@FindBy(xpath = "//*[@id="full-width-tabpanel-any"]/div/div[1]/div[1]/div/div/div/div[2]/div")
    WebElement progressBar;

    //@FindBy(xpath = "/html/body/div[3]/div[3]/div/div[1]/button/span/svg/path")
    WebElement closePopUp;

    public DataDetailViewPage() { PageFactory.initElements(driver, this); }

     * public DataDetailViewPage clickDataTab() {
     * 
     * // Thread.sleep(1000);
     * 
     * // dataTab.click();
     * 
     * try { Thread.sleep(5000); System.out.println("Waiting time Data Btn"); }
     * catch (InterruptedException e) { e.printStackTrace(); }
     * 
     * JavascriptExecutor js = (JavascriptExecutor) driver;
     * js.executeScript("arguments[0].click();", dataTab);
     * 
     * return new DataDetailViewPage(); }
    

    public DataDetailViewPage clickDataCard() {

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    dataCard = driver.findElement(By.xpath("//*[@id=\"full-width-tabpanel-1\"]/div/div/div[1]/div/div[1]/div/div[2]"));
    
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].click();", dataCard);
    
    //dataCard.click();
    
    return new DataDetailViewPage();
    

    }

    public DataDetailViewPage validateDataCardPopUp() {

    detailsPopup = driver.findElement(By.xpath("/html/body/div[3]/div[3]"));
    
    String handler = driver.getWindowHandle();
    
    Set handlers = driver.getWindowHandles();
    
    boolean elementPresent = false;
    
    Iterator iterator = handlers.iterator();
    
    while (iterator.hasNext()) {
    
        String popUps = iterator.next().toString();
    
        if (!popUps.contains(handler)) {
    
            driver.switchTo().window(popUps);
    
            elementPresent = detailsPopup.isDisplayed();
    
            Assert.assertTrue(elementPresent);
    
        } else {
            Assert.assertFalse(elementPresent);
    
        }
    }
    return new DataDetailViewPage();
    

    }

    public DataDetailViewPage validateScrollbar() {

    detailsPopup = driver.findElement(By.xpath("/html/body/div[3]/div[3]"));
    
    String script = "return document.documentElement.scrollHeight>document.documentElement.clientHeight;";
    
    JavascriptExecutor scrollBarPresent = (JavascriptExecutor) driver;
    
    Boolean scroll = (Boolean) (scrollBarPresent.executeScript(script));
    
    int elementCount = detailsPopup.findElements(By.className("py-3 card-body")).size();
    
    if (elementCount >= 3) {
        Assert.assertTrue(scroll);
    } else {
        Assert.assertFalse(scroll);
    }
    return new DataDetailViewPage();
    

    }

    public DataDetailViewPage validateProgressBarColour() {

    progressBar = driver.findElement(By.xpath("//*[@id=\"full-width-tabpanel-any\"]/div/div[1]/div[1]/div/div/div/div[2]/div/div"));
    
    String colour = progressBar.getCssValue("background-color");
    
    String actualColour = Color.fromString(colour).asHex();
    
    String expectedColour = "#880066";
    
    Assert.assertEquals(actualColour, expectedColour);
    
    return new DataDetailViewPage();
    

    }

    public DataDetailViewPage validateCloseDetailPopup() {

    closePopUp = driver.findElement(By.xpath("/html/body/div[3]/div[3]/div/div[1]/button/span/svg/path"));
    
    closePopUp.click();
    
    Assert.assertTrue(detailsPopup.isDisplayed());
    
    return new DataDetailViewPage();
    

    }

    }

    This is my test class

    public class DataDetailViewTest extends TestBase {
    

    DataDetailViewPage dataPage;

    public DataDetailViewTest() {
    super();
    }

    @BeforeTest
    public void setUpDataView() {
    dataPage = new DataDetailViewPage();
    }

    @Test
    public void verifyDataDetailView(){

    dataPage = dataPage.clickDataTab();
    
      dataPage = dataPage.clickDataCard();
      
      dataPage = dataPage.validateDataCardPopUp();
      
      dataPage = dataPage.validateScrollbar();
      
      dataPage = dataPage.validateProgressBarColour();
      
      dataPage = dataPage.validateCloseDetailPopup();
    

    }

    }

    compilation is stuck at Trying to find Element By : By.xpath: //*[@id="full-width-tabpanel-1"]/div/div/div[1]/div/div[1]/div/div[2]

    I tried locating element with @FindBy and css selector as well. but cannot go further.

    How can I locate element without being stuck and move forward?



  • Issue was in xpath. Selenium could not able to locate eliment with given xpath. Threfore I customize the xpath use findElements to identify all similar elements. Then use get index to get specific element.

    driver.findElements(By.xpath("//div[@class='text-center justify-content-center']")).get(0);
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2