1

Given an XML structure like so:

<table>
    <thead>
        <tr>    
            <th><div data-col="0">First Name</div></th>
            <th><div data-col="1">Last Name</div></th>
            <th><div data-col="2">Age</div></th>
            <th><div data-col="3">Id</div></th>
            <th><div data-col="4">City</div></th>
        </tr>
    </thead>
    <tbody>
        <tr data-row="0">
            <td data-col="0"><div>Ivan</div></td>
            <td data-col="1"><div>Ivanov</div></td>
            <td data-col="2"><div>35</div></td>
            <td data-col="3"><div>EFF-12218</div></td>
            <td data-col="4"><div>London</div></td>
        </tr>
    </tbody>
</table>
  1. How could I get the value of data-col (where text = 'id'), for the first element?
  2. How could I get the value of text in this cell ('EFF-12218') using found value of data-col ?
kjhughes
  • 106,133
  • 27
  • 181
  • 240
NeverSleeps
  • 1,439
  • 1
  • 11
  • 39

4 Answers4

2

To retrieve the value of data-col where text is id you can use either of the following Locator Strategies:

  • Using XPATH:

    print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
    

Ideally, you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following solution:

  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

To retrieve the value EFF-12218 with respect to data-col="3" you can use the following solution:

  • Using XPATH:

    data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col")
    print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
    

Ideally, you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following solution:

  • Using XPATH:

    data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1
  1. How could I get the value of data-col (where text = 'id'), for the first element?

String colNumber = driver.findElement(By.xpath("//div[text()='Id']")).getAttribute("data-col");

  1. And how could I get the value of text in this cell ('EFF-12218') using found value of data-col ?

driver.findElement(By.xpath("//td[@data-col='" + colNumber + "']")).getText();

Sooraj
  • 565
  • 3
  • 8
0

To use the value of the attribute data-col as an index to the children of the <tbody> element, use this XPath-1.0 expression

//tbody/tr/td[@data-col=//thead/tr/th[div/text()='Id']/div/@data-col]

Its result is

EFF-12218

as expected.
The answer to your first question is, by the way, the following

//thead/tr/th[div/text()='Id']/div/@data-col

which is a part of the second question.

zx485
  • 28,498
  • 28
  • 50
  • 59
0

Refer below response it will help you to get text of any cell. you have to just change value of row and column in xpath:

 WebDriver driver = new ChromeDriver();
 // to Get Text of ID( you can store into array/arraylist
// By Changing the row column value in xpath //tbody/tr[row]/td[column]) you can get 
   //data of any cell of table
List<WebElement> id_elements = driver.findElements(By.xpath("//tbody/tr/td[4]"));
int noOfRow= id_elements.size();
for(int i=0; i<=noOfRow;i++) {
    String id = driver.findElement(By.xpath("//tbody/tr["+i+"]/td[4]")).getText();
    System.out.println(id);
    }

Hope it gives you idea!

Ajeet Yadawa
  • 135
  • 3
  • 16