Home > Back-end >  All though Xpath is showing all elements in selector hub from expected div ,selenium isn't capt
All though Xpath is showing all elements in selector hub from expected div ,selenium isn't capt

Time:12-20

I want to capture all the headers of a table using the selenium using the. I am using the Xpath to capture the webelement using Xpath. Xpath:

"//div[@class='dataTables_scrollHeadInner']//tr[@id='report-data-table-header-0']/th"

and it shows all the elements of the div i.e (9 th's),but when I capture it using selenium I only get 7 elements.

All the xpaths are mentioned are required the and the commented block of code is function call of homePage.view_report_values(textString).

Source Code:

package Onsight.Framework;
import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import com.github.javafaker.Faker;

import avdhut.onsight.commonutils.BaseComponenets;
import avdhut.onsight.pageobject.HomePage;
import avdhut.onsight.pageobject.LoginPage;
import io.github.bonigarcia.wdm.WebDriverManager;

public class Webtable  {
    
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        
        String urlString = "https://oinsightsvm1.westindia.cloudapp.azure.com:444/ctsOInsightsBMS/res/createReport.html";
        String userEmailString = "User66";
        String userPassword = "Avdhut@5201";
        String titleString;
        String textString="Clarita";
        Faker faker = new Faker();
        WebDriverManager.chromedriver().setup();
        ChromeOptions options = new ChromeOptions();
        // options.addArguments("--ignore-ssl- errors=yes");
        options.addArguments("--ignore-certificate-errors");
        WebDriver driver = new ChromeDriver(options);
        driver.get(urlString);
        LoginPage loginPage = new LoginPage(driver);
        loginPage.login_username(userEmailString);
        loginPage.click0nsubmit();
        loginPage.EnterPassword(userPassword);
        loginPage.click0nsubmit();
        HomePage homePage = new HomePage(driver);
        homePage.view_report_values(textString);
           
        
    
    
    }
}
//tr[class='odd'] em[class='fa fa-table']

Page Object file:

[![public class HomePage extends BaseComponenets {
    WebDriver driver;
    By createReportBy=By.cssSelector("button\[data-target*='#addReportModal'\]");
    By baseTableBy=By.xpath("//table\[@id='query-table'\]");
    By report_name_rowsBy=By.xpath("//tbody/tr");
    By viewBy=By.xpath("//button\[contains(@title,'View')\]");
    By view_dataBy=By.xpath("//button\[contains(@title,'View data')\]");
    By dataBy=By.xpath("//table\[@id='reportDataTable-0'\]/tbody/tr ");
    By teable_headingBy=By.xpath("//div\[@class='dataTables_scrollHeadInner'\]//tr\[@id='report-data-table-header-0'\]/th");
    By next_paginateBy=By.xpath("//li\[@id='reportDataTable-0_next'\]");
    By paginateBy=By.xpath("//div\[@id='reportDataTable-0_paginate'\]/ul\[@class='pagination'\]/li\[@class='paginate_button page-item '\]");
    By no_of_pagesBy=By.xpath("//div\[@id='reportDataTable-0_info'\]");
    public HomePage(WebDriver driver) {
        super(driver);
        // TODO Auto-generated constructor stub
        this.driver=driver;
    }
    public void CreateReport_Button() {
        IsClickable(createReportBy);
        WebElement create_Report=driver.findElement(createReportBy);
        create_Report.click();
    }
    public void get_headers() {
        IsVisible(teable_headingBy);
        List<WebElement> table_headerElement=driver.findElements(teable_headingBy);
        
            for (WebElement header : table_headerElement) {
                System.out.println(header.getText());
            }
        
        
        
    }
    public void view_report_values(String report_title) throws InterruptedException {
    
        IsVisible(baseTableBy);
        WebElement baseTable=driver.findElement(baseTableBy);
        List<WebElement> table_roWebElement=baseTable.findElements(report_name_rowsBy);
        List<String> list=new ArrayList<String>();
        for (WebElement webElement : table_roWebElement) {
            if (webElement.getText().contains(report_title)) {
                System.out.println(true);
                IsClickable(viewBy);
                System.out.println(webElement.getText());
                webElement.findElement(viewBy).click();
                IsClickable(view_dataBy);
                driver.findElement(view_dataBy).click();    
                IsVisible(next_paginateBy);
                List<WebElement> pagebar=driver.findElements(paginateBy);
                for(WebElement page:pagebar) {
                    list.add(page.getText());
                }
//          name(list);
                if (driver.findElement(next_paginateBy).isDisplayed()) {
                    for (int i = 1; i <= 30; i  ) {
                        if (i==1) {
                            System.out.println("first_page");
                            get_headers();
                        }
                        
//                      Thread.sleep(10000);
                        
                   }
                } else {

                }
                
//                      
        }
}][1]][1]

Console O/P:

TIMESTAMP
SOURCE NAME
SOURCE STATE
ACK STATE
MESSAGE TEXT
LAST NOTES

After that I googled and then found some articles and then In order to test I used console of google chrome

document.getElementsByClassName('dataTables_scrollBody').scrollLeft  = 250", "

It gave me an error , then, I used:

document.getElementsByClassName('dataTables_scrollBody').scrollLeft  =250

Then, I got the message :

NaN

enter image description here This is the table I want to scroll also the classname mentioned in belongs to same div of the table in image

CodePudding user response:

After login, add the below code and try:

driver.findElement(By.xpath(".//button[@class='toast-close-button']")).click();
            
driver.findElement(By.xpath("(.//td[text()='random']//parent::tr//td/button)[1]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("(.//div[@class='modal-content']//button[@type='button' and @title='View data'])[1]")).click();
Thread.sleep(2000);
            
JavascriptExecutor js=(JavascriptExecutor)driver;
            
List<WebElement> headers = driver.findElements(By.xpath(".//div[@class='dataTables_scrollHead']//th"));
System.out.println("Total no. of headers: "   headers.size());
            
System.out.println("Headers: ");
System.err.println("========");
for (WebElement header : headers) {
    js.executeScript("arguments[0].scrollIntoView(true);", header);
    System.out.println(header.getText());
}

Output:

Total no. of headers: 9
Headers: 
========
TIMESTAMP
SOURCE NAME
SOURCE STATE
ACK STATE
MESSAGE TEXT
LAST NOTES
ALL NOTES
ALARM VALUE
LOW LIMIT

I answered your previous question also, check that, if it works for you, mark that as answer.

  • Related