java 我想访问表中嵌套td的内容

vjrehmav  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(94)

大家好,我正在使用 selenium ,并希望点击表中的按钮,以访问值,但为了做到这一点,我必须访问一个嵌套在td标记中的按钮。我想验证报告中生成的值。我已经使用了一种方法,但它给我错误,请帮助我。随时执行代码,只是不要误用
代码:

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.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;
        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);
        Thread.sleep(10000);
         WebElement baseTable = driver.findElement(By.xpath("//table"));//*
    List<WebElement> table_row_1=baseTable.findElements(By.xpath("*[@id='query-table']/tbody/tr/td[2]"));
    for (WebElement webElement : table_row_1) {
        if (webElement.getText().equalsIgnoreCase("random")) {
            System.out.println(webElement.getText());
            webElement.findElement(By.xpath("//[@id='query-table']/tbody/tr/td[7]/button[@title='View'"));
        } else {
            System.out.println(webElement.getText());   
            driver.close();                           //table code
        }
        
    }
        
        ////table[@id='query-table']/tbody/tr/td[7]/button[@title='View']

    }

}

HTML代码:

<table id="query-table" class="table table-striped dataTable no-footer" role="grid" aria-describedby="query-table_info" xpath="1" style="">
<tbody xpath="1">
<tr class="odd">
                    <td>1234</td>
                    <td>random</td>
                    <td>9-Dec-2022 15:16:51</td>
                    <td>
                    User66
                    </td>
                    <td>private</td>
                    <td>Not Scheduled</td>
                    <td>// Want to access this td 
                        <button type="button" title="View" rel="tooltip" class="btn btn-info btn-link btn-icon btn-sm" onclick="openJobModal('5d452186-d034-4c98-81ff-ff2ebfeaf165','true')">//this button

                                            
                    </td>
                    
                </tr>
m1m5dgzv

m1m5dgzv1#

它应该像这样使用xpath:

//button[@class='btn btn-info btn-link btn-icon btn-sm']

//table//button[@class='btn btn-info btn-link btn-icon btn-sm']

相关问题