简体   繁体   English

如何使用Selenium Webdriver Java查找表行号

[英]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);

print 打印

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.

相关问题 使用Selenium WebDriver查找表行号 - Find a table row number using selenium webdriver 如何遍历表以查找第 1 列中的字符串匹配项,然后使用 Selenium WebDriver Java 在另一列的同一行上选择下拉列表? - How to loop through table to find a string match in column 1, then select a dropdown on the same row in another column using Selenium WebDriver Java? 如何使用 Selenium Java 在表中查找元素的行索引 - How to find the row index of an element in a table using Selenium Java 硒webdriver获取表中的可见行号 - selenium webdriver to get visible row number in table 如何使用 Selenium Webdriver - Java 查找网页上的复选框总数? - How to find the total number of checkboxes present on web page using Selenium Webdriver - Java? 如何使用 Selenium Webdriver 和 Java 在同一行中单击旁边/上一个表格单元格(TD)? - How to click on the aside / previous Table Cell (TD) in the same row using Selenium Webdriver with Java? Selenium Webdriver Java:如何使用行号和列号单击表中的特定单元格 - Selenium Webdriver Java: How to click on a particular cell in table using row and column numbers 如何在Java中使用Selenium WebDriver查找特定的表元素? - How to find specific table element with selenium webdriver in java? 如何使用Java在Selenium WebDriver中获取Web表中的确切行数 - How to get the exact number of rows in webtable in Selenium WebDriver using Java 如何使用带有 Java 的 Selenium WebDriver 查找损坏的链接 - How to find broken links using Selenium WebDriver with Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM