1

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.

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @Saifur. I tried it but it returns error org.openqa.selenium.NoSuchElementException: no such element. Seems the driver.findElement(By.cssSelector("[data-job='" + job + "']")); cannot find the element. If I change to driver.findElement(By.id("20631697"));, then it can find it, but I can't depend on the ID.
Try this //since you know the job String job = "A01763431 Job YYY"; String dataRowIndex = driver.findElement(By.cssSelector("#ScheduleTable-01 tr[data-job='" + job + "']")).getAttribute("data-row-index"); System.out.println(dataRowIndex);
@keylogger U can also use xpath as //tr[contains(@data-job,'"+job+"')] and then use getAttribute to fetch dataRowIndex.
@Saifur I still get org.openqa.selenium.NoSuchElementException: no such element @Vivek Singh I get org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //tr[contains(@data-job,'A01763431 Job YYY')]) because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//tr[contains(@data-job,'A01763431 Job YYY')])' is not a valid XPath expression.
You have an odd ")" in the end of xpath
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.