Home > Net >  I want to access contents of a nested td within a table
I want to access contents of a nested td within a table

Time:12-17

hi guys I am using selenium and want to click on a button in a table in order to access the values but in order to do that I have to access a buton nested in td tag. I want to verify verify the values generated in report . I have used an approach but its giving me error please help me. feel free to execute the code, just dont mis-use

Code:

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 code:

<table id="query-table"  role="grid" aria-describedby="query-table_info" xpath="1" style="">
<tbody xpath="1">
<tr >
                    <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"  onclick="openJobModal('5d452186-d034-4c98-81ff-ff2ebfeaf165','true')">//this button

                                            
                    </td>
                    
                </tr>

CodePudding user response:

It should work with xpath like this:

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

or

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