java 尽管Xpath显示了selector hub中来自预期div的所有元素,但selenium并没有完全捕获

8ehkhllq  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(125)

我想使用selenium捕获一个表的所有标题,使用. I am use the Xpath捕获webelement使用Xpath. Xpath:

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

它显示了div的所有元素,也就是第九个元素,但是当我用 selenium 捕捉它时,我只得到了7个元素。
所有提到的xpath都是必需的,注解代码块是homePage. view_report_values(textString)的函数调用。
源代码:

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']

页面对象文件:

[![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]

控制台O/P:

TIMESTAMP
SOURCE NAME
SOURCE STATE
ACK STATE
MESSAGE TEXT
LAST NOTES

之后,我谷歌,然后找到了一些文章,然后为了测试,我用谷歌浏览器的控制台

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

它给了我一个错误,然后,我使用:

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

然后,我得到了一个信息:
钠氮


这是我想要滚动的表,中提到的类名也属于图像中表的同一div

guicsvcw

guicsvcw1#

登录后,添加以下代码并尝试:

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

输出:

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

我也回答了你的前一个问题,检查,如果它为你工作,标记为答案。

相关问题