简体   繁体   中英

How to find the table row number using Selenium Webdriver Java

I have a table with multiple rows and multiple columns.

The HTML codes look like this:

<!-- language: lang-html -->
    <div id="ScheduleTable-01" class="widget Scheduletable suppress-errors Schedule-grid" data-widget="ScheduleTable">
    <div class="grid-wrapper">
    <table class="nostyles weekmode hourstype fullmonth" style="width: 100%;">
    <thead>
    <tbody>
    <tr id="20631697" class="Schedule-row row0 20631697 key_AuthoriserId-1077_JobId-402704200_TaskId-CON_TsCode-35" data-row-index="0" data-job="402121200,Job XXX">
    <tr id="-499545938" class="Schedule-row row1 -499545938 key_AuthoriserId-1077_JobId-A01200131S_TaskId-CON_TsCode-35" data-row-index="1" data-job="A01763431 Job YYY">
    <tr id="-985929934" class="Schedule-row row2 -985929934 key_AuthoriserId-1277_JobId-I02010171S_TaskId-INT_TsCode-30" data-row-index="2" data-job="I02872371 S,Job ZZZ">

Because it is a dynamic webpage, every time the page loads, Job YYY will be placed in different row index. Thus, I want to know in which row of the table Job YYY is located. I can see that each row is marked with data-row-index , that is what I want to get.

I am thinking about this Selenium code

<!-- language: lang-java -->
WebElement mainTable = driver.findElement(By.id("ScheduleTable-01"));
//I am not sure about this part below; findElements By ???
List<WebElement> tableRows = mainTable.findElements(By.tagName("tr"));

In this case, how can I find the row number? Thanks.

You can easily use getAttribute() See the api doc . getAtribute() allows you to get atribute value of any html tag

//since you know the job
String job = "A01763431 Job YYY";

String dataRowIndex = driver.findElement(By.cssSelector("[data-job='" + job + "']")).getAttribute("data-row-index");

System.out.println(dataRowIndex);

print

1

Edit

Ok, there could be couple of things impacting this.

The element is inside an iframe if so, then use

driver.switchTo().frame(driver.findElement(By.cssSelector("css for iframe")));

You are looking for the element too fast. Use explicit wait.

String job = "A01763431 Job YYY";
By selector = By.cssSelector("#ScheduleTable-01 tr[data-job='" + job + "']");
WebElement element = new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(selector));
String dataindex = element.getAttribute("data-row-index");

More than one elements are being returned by the selector provided

String job = "A01763431 Job YYY";
By selector = By.cssSelector("[data-job='" + job + "']");

List<WebElement> elements = new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfAllElementsLocatedBy(selector));
int size = elements.size();
System.out.println(size);

See how many was returned

I tried this one here and it solves the problem.

<!-- language: lang-java -->
    String job = "YYY";
    WebElement table = driver.findElement(By.xpath("//tr[contains(@data-job,'"+job+"')]")); 
    String dataRowIndex = table.getAttribute("data-row-index");
    System.out.println(dataRowIndex);

Basically I just take a part of the job ID ( YYY instead of the full name), and use contains() in the xpath.

@Vivek @Saifur Thanks a lot for the suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM