How to find specific lines in a table using Selenium?

前端 未结 6 416
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 09:52

Here is an example code:

So this table being in prod

相关标签:
6条回答
  • 2021-01-02 10:24

    if you want to access table cell

    WebElement thirdCell = driver.findElement(By.Xpath("//table/tbody/tr[2]/td[1]")); 
    

    If you want to access nested table cell -

    WebElement thirdCell = driver.findElement(By.Xpath("//table/tbody/tr[2]/td[2]"+//table/tbody/tr[1]/td[2]));
    

    For more details visit this Tutorial

    0 讨论(0)
  • 2021-01-02 10:25

    you can try following

    int index = 0;
    WebElement baseTable = driver.findElement(By.className("table gradient myPage"));
    List<WebElement> tableRows = baseTable.findElements(By.tagName("tr"));
    tableRows.get(index).getText();
    

    You can also iterate over tablerows to perform any function you want.

    0 讨论(0)
  • 2021-01-02 10:31

    The following code allows you to specify the row/column number and get the resulting cell value:

    WebDriver driver = new ChromeDriver();
    WebElement base = driver.findElement(By.className("Table"));
    tableRows = base.findElements(By.tagName("tr"));
    List<WebElement> tableCols = tableRows.get([ROW_NUM]).findElements(By.tagName("td"));
    String cellValue = tableCols.get([COL_NUM]).getText();
    
    0 讨论(0)
  • 2021-01-02 10:34

    Well previously, I used the approach that you can find inside the WebElement:

    WebElement baseTable = driver.findElement(By.tagName("table"));
    WebElement tableRow = baseTable.findElement(By.xpath("//tr[2]")); //should be the third row
    webElement cellIneed = tableRow.findElement(By.xpath("//td[2]"));
    String valueIneed = cellIneed.getText();
    

    Please note that I find inside the found WebElement instance.

    The above is Java code, assuming that driver variable is healthy instance of WebDriver

    0 讨论(0)
  • 2021-01-02 10:43

    You want:

    int rowNumber=...;
    string value = driver.findElement(By.xpath("//div[@id='productOrderContainer']/table/tbody/tr[" + rowNumber +"]/div[id='something']")).getText();
    

    In other words, locate <DIV> with the id "something" contained within the rowNumberth <TR> of the <TABLE> contained within the <DIV> with the id "productOrderContainer", and then get its text value (which is what I believe you mean by "get me the value in <div id='something'>"

    0 讨论(0)
  • 2021-01-02 10:47
    (.//*[table-locator])[n]
    

    where n represents the specific line.

    0 讨论(0)
提交回复
热议问题