[英]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: HTML代码如下所示:
<!-- 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. 由于它是动态网页,因此每次加载页面时, 作业YYY都会放置在不同的行索引中。 Thus, I want to know in which row of the table Job YYY is located.
因此,我想知道作业YYY在表的哪一行。 I can see that each row is marked with
data-row-index
, that is what I want to get. 我可以看到每一行都标记有
data-row-index
,这就是我想要得到的。
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 . 您可以轻松使用
getAttribute()
参见api doc 。 getAtribute()
allows you to get atribute
value of any html
tag getAtribute()
允许您获取任何html
标记的atribute
值
//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);
打印
1 1个
Edit 编辑
Ok, there could be couple of things impacting this. 好的,可能有几件事会影响到这一点。
The element is inside an iframe
if so, then use 如果是,则该元素位于
iframe
,然后使用
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. 基本上,我只使用作业ID的一部分(用YYY代替全名),然后在xpath中使用contains()。
@Vivek @Saifur Thanks a lot for the suggestions. @Vivek @Saifur非常感谢您的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.