Prabu
Prabu

Reputation: 3728

getting div id value using webdriver with java

From the below HTML source I want to get the table's id value (DataTables_Table_1) under the priority section, because this id generate dynamically like (DataTables_Table_1,DataTables_Table_2,DataTables_Table_2)

<div class="box light detailsBox " id="priority" rel="Priority">
    <div class="box-content detailsBoxBody">
        <div id="DataTables_Table_1_wrapper" class="dataTables_wrapper" role="grid">
            <table class="ohim-table  dataTable" data-filter="" rel="dataTable1515477181791" id="DataTables_Table_1" aria-describedby="DataTables_Table_1_info">
            </table>
        </div>
    </div>
</div>

below is my Java source code:

first I moved to priority div tag from that div, I moved inside the with class attribute

WebElement checPri = driver.findElement(By.id("priority"));
String insideDiv = checPri.findElement(By.className("dataTables_wrapper")).getAttribute("id");

I'm getting the output but not as expected one, because className applied multiple times, so its pick some other id not pick under the priority div.

Please guide me.

Upvotes: 1

Views: 764

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193108

To retrive the table id values (e.g. DataTables_Table_1) under the priority section you can use the following line of code :

System.out.println(driver.findElement(By.xpath("//div[@id='priority']//table[@class='ohim-table  dataTable']")).getAttribute("id"));    

Upvotes: 1

Guy
Guy

Reputation: 50854

You can use cssSelector to specify the the path

WebElement checPri = driver.findElement(By.cssSelector("#priority .dataTables_wrapper"));
String insideDiv = checPri.getAttribute("id");

Or to look for an element with class dataTables_wrapper and partial id DataTables_Table

WebElement checPri = driver.findElement(By.id("priority"));
String insideDiv = checPri.findElement(By.cssSelector("[id*='DataTables_Table'].dataTables_wrapper")).getAttribute("id");

Upvotes: 2

Related Questions