Java variable name in findElement(By.cssSelector....

1,804 views
Skip to first unread message

bsri

unread,
Jan 14, 2013, 8:29:44 PM1/14/13
to webd...@googlegroups.com
I am trying to locate an access string, which can be "High", "Medium", or "Low" on the UI. These are attributes of a file, within a folder - I receive both of them as parameters to this method:
public static String getSecurityAccess(WebDriver webdriver, WebElement folder_name, WebElement file_name) {

String folder_element_id=folder_name.getAttribute("id");  // reach the folder

WebElement file_table=webdriver.findElement(By.id(folder_element_id.replaceAll("name","table"))).findElement(By.tagName("table")); // this would table me to the html table which contains all files under this folder. All tables that hold the files under a folder have been names in such a way that replacing the string "name" in the folder name with "table" would take me to the table

WebElement file_table_body=file_table.findElement(By.tagName("tbody")); // the table body contains files and their attributes

The tbody would contain the string - "High" or "Medium" or "Low" for each file and would be named using the file name; i.e. if the file name is file-name-hello.pdf, the access string would be file-access-hello.pdf, i.e. I need to substitute file-name- with file-access- and I get the id for the access string within the tbody. This can be done with a string function in Java but the output would be a  Java variable name. How would I use the Java variable name in a By.cssSelector?

WebElement access_element = file_table_body.findElement(By.cssSelector("td[id=< I should put the Java variable name here>]");

What is the syntax for the cssSelector parameter?

Thanks,

 

 

}

 

 

Krishnan Mahadevan

unread,
Jan 15, 2013, 5:40:35 AM1/15/13
to webd...@googlegroups.com


The following should help. 


String fooLocator; //lets assume this is your locator. 

WebElement access_element = file_table_body.findElement(By.cssSelector("td[id=" + fooLocator + "]");

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/zcPY_ZcS018J.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

Mike Riley

unread,
Jan 15, 2013, 2:02:32 PM1/15/13
to webd...@googlegroups.com
It would help us visualize this better if you provided example HTML code.  Why are you insisting that you use a CSS locator when you don't know how to use them?  Maybe another locator would be better or easier. Again, example HTML please.

As for how to use the string you end up with, you simply add the string where it would go if you had it as a constant:
"constant string part " + stringVariable + " trailing constant string (if any)"

Mike

bsri

unread,
Jan 15, 2013, 7:09:17 PM1/15/13
to webd...@googlegroups.com

<div id="table-container"> // A table for all files within a folder
 <div id="accordion">
     <div>
   <table id="folder-header-15229" class="dir-folder-table">
       <thead class="align_left">
                  <th class="folder-name" id="folder-name-15229">Financial</th>
          </thead>
          </table>
        </div>

     <div id="folder-table-15229">
      <table id="fileTable" class="dir-folder-table" style="background-color:#b5c7db; border-collapse:collapse; ">
       <colgroup>
        <col width="30px" />
        <col width="150px" word-wrap: break-word/>
        <col width="50px" />
        <col width="70px" />
        <col width="100px" />
        <col width="130px" />
        <col width="140px" />
       </colgroup>
                              <thead>
         <tr>
        <th class="center"></th>
        <th class="file-name"><a href="javascript:sortFiles(15229, 'td.file-name')">File Name</a></th>
        <th class="center file-type"><a href="javascript:sortFiles(15229, 'td.file-type')">Type</a></th>
        <th class="center file-access"><a href="javascript:sortFiles(15229, 'td.file-access')">Access</a></th>
        <th class="center file-size-raw"><a href="javascript:sortFiles(15229, 'td.file-size-raw', true)">Size</a></th>
        <th class="center file-modified-ts"><a href="javascript:sortFiles(15229, 'td.file-modified-ts', true)">Last Modified</a></th>
        <th class="center"></th>
         </tr>
         </thead>
         <tbody>
        <tr class="selectable" style="background-color:whiteSmoke;" onmouseover="javascript:showPreview(15246);">
         <td class="center" style="padding-top:5px;">
          <input name="fileId" type="checkbox" value="15246-2">
         </td>
         <td class="file-name" id="file-name-865d32da_Blank.docx"><p class="ellipsis"><span class="emphasize">865d32da_Blank.docx</span></p></td>
         <td class="center file-type">DOCX</td>
         <td class="center file-access">Medium</td>  I will need to access this for the file I use in my test script
         <td class="file-size-raw" style="display: none;">28934</td>
         <td class="center file-size">28.3 KB</td>
         <td class="file-modified-ts" style="display: none;">1358210484728</td>
         <td class="center file-modified">2013-01-14 16:41</td>
         <td class="center">
          <a href="javascript:renameFile(15246)">Rename</a> I will need to access this per file in my test script
           <a href="javascript:downloadFile(15246, 2)">Download</a> I will need to access this per file in my test script
          </td>
          <tr><td colspan="8"><div class="line"></div></td></tr>
        </tr>
       </tbody>
       .
       .
       .
       .
       More <table>s if there are more files in this folder
      </table>
     </div>
        <div>
      <table id="folder-header-15228" class="dir-folder-table">
       <thead class="align_left">
                    <th class="folder-name" id="folder-name-15228">Medical</th>
       </thead>
      </table>
        </div>
     <div id="folder-table-15228">
      <table id="fileTable" class="dir-folder-table" style="background-color:#b5c7db; border-collapse:collapse; ">
       <colgroup>
        <col width="30px" />
        <col width="150px" word-wrap: break-word/>
        <col width="50px" />
        <col width="70px" />
        <col width="100px" />
        <col width="130px" />
        <col width="140px" />
       </colgroup>
                              <thead>
         <tr>
        <th class="center"></th>
        <th class="file-name"><a href="javascript:sortFiles(15228, 'td.file-name')">File Name</a></th>
        <th class="center file-type"><a href="javascript:sortFiles(15228, 'td.file-type')">Type</a></th>
        <th class="center file-access"><a href="javascript:sortFiles(15228, 'td.file-access')">Access</a></th>
        <th class="center file-size-raw"><a href="javascript:sortFiles(15228, 'td.file-size-raw', true)">Size</a></th>
        <th class="center file-modified-ts"><a href="javascript:sortFiles(15228, 'td.file-modified-ts', true)">Last Modified</a></th>
        <th class="center"></th>
         </tr>
         </thead>
         <tbody>
        <tr class="selectable" style="background-color:whiteSmoke;" onmouseover="javascript:showPreview(15282);">
         <td class="center" style="padding-top:5px;">
          <input name="fileId" type="checkbox" value="15282-2">
         </td>
         <td class="file-name" id="file-name-4ef5d145_WelcomeFax.tif"><p class="ellipsis"><span class="emphasize">4ef5d145_WelcomeFax.tif</span></p></td>
         <td class="center file-type">TIF</td>
         <td class="center file-access">Low</td>
         <td class="file-size-raw" style="display: none;">89534</td>
         <td class="center file-size">87.4 KB</td>
         <td class="file-modified-ts" style="display: none;">1358210588671</td>
         <td class="center file-modified">2013-01-14 16:43</td>
         <td class="center">
          <a href="javascript:renameFile(15282)">Rename</a>
          <a href="javascript:downloadFile(15282, 2)">Download</a>
         </td>
          <tr><td colspan="8"><div class="line"></div></td></tr>
        </tr>
        <tr class="selectable" style="background-color:whiteSmoke;" onmouseover="javascript:showPreview(15268);">
         <td class="center" style="padding-top:5px;">
          <input name="fileId" type="checkbox" value="15268-1">
         </td> 
         <td class="file-name" id="file-name-f1f61413_DataSheetv3.pdf"><p class="ellipsis"><span class="emphasize">f1f61413_PayeBillDataSheetv3.pdf</span></p></td>
         <td class="center file-type">PDF</td>
         <td class="center file-access">High</td>
         <td class="file-size-raw" style="display: none;">659785</td>
         <td class="center file-size">644.3 KB</td>
         <td class="file-modified-ts" style="display: none;">1358210546909</td>
         <td class="center file-modified">2013-01-14 16:42</td>
         <td class="center">
          <a href="javascript:renameFile(15268)">Rename</a>
          <a href="javascript:downloadFile(15268, 1)">Download</a>
         </td>
         <tr><td colspan="8"><div class="line"></div></td></tr>
        </tr>
        .
        .
        .
 
My Java methods code to access a random folder and a random file:

// Get list of folders

public static List<WebElement> getFolderList(WebDriver webdriver) {

WebElement folder_table_container=webdriver.findElement(By.cssSelector(

"div[id='table-container']"));

List<WebElement> folder_list=folder_table_container.findElements(By.cssSelector(

"th[id^='folder-name-']"));

return folder_list;

}

// Get a random folder from folder list

public static WebElement getRandomFolderElement(WebDriver webdriver) {

List<WebElement> folder_list=getFolderList(webdriver);

if(folder_list.size()==0)

return null;

else {

int i = new Random().nextInt(folder_list.size());

return folder_list.get(i);

}

}

// Get list of files in a folder

public static List<WebElement> getFileList(WebDriver webdriver, WebElement folder_element) {

String folder_element_id=folder_element.getAttribute(

"id");

WebElement file_table=webdriver.findElement(By.id(folder_element_id.replaceAll(

"name", "table"))).findElement(By.tagName("table"));

WebElement file_table_body=file_table.findElement(By.tagName(

"tbody"));

List<WebElement> file_list=file_table_body.findElements(By.cssSelector(

"td[id^='file-name-']"));

return file_list;

}

// Get a random file from a given folder

public static WebElement getRandomFileElement(WebDriver webdriver, WebElement folder_element) {

List<WebElement> file_element_list = getFileList(webdriver,folder_element);

if (file_element_list.size()==0)

return null;

else {

int i = new Random().nextInt(file_element_list.size());

return file_element_list.get(i);

}

}

Given a random file I get from getRandomFileElement(), I also need the Access value of that file.

WebElement access_element = file_table_body.findElement(By.cssSelector("td[id=< I should put the Java variable name here>]");

Thanks,


         
On Monday, January 14, 2013 5:29:44 PM UTC-8, bsri wrote:

bsri

unread,
Jan 15, 2013, 7:27:35 PM1/15/13
to webd...@googlegroups.com

I tried coding using absolute values; for the “Financial” folder. Here is my webdriver method:

public static void testClassAccess(WebDriver webdriver) {

              webdriver.findElement(By.id("folder-name-15229")).click(); // click on Financial folder name using id

              WebElement folder_table_container=webdriver.findElement(By.cssSelector("div[id='table-container']")); // Find the table-container div

              WebElement folder_table=folder_table_container.findElement(By.id("folder-table-15229")); // This is the table for all files under Financial. I substitute “name” with “table” to get it when I generalize the code

              WebElement folder_table_tr=folder_table.findElement(By.tagName("tbody")).findElement(By.cssSelector("tr[class='selectable']")); // Got to the tbody and then the tr element under folder_table

              //WebElement test_file=folder_table_tr.findElement(By.cssSelector("td[id='file-name-865d32da_Blank.docx']"));  // When I tried to print this out – it worked. System.out.println output the name of the file on the UI.

              WebElement test_file_access=folder_table_tr.findElement(By.cssSelector("td[class='file-access']")); // This is not working; I get “NoSuchElementFoundException”. The class name is “center file-access”

              // System.out.println(test_file.getText()); // This works. I get 865d32da_Blank.docx displayed

              System.out.println(test_file_access.getText()); // This doesn’t work.

Mike Riley

unread,
Jan 16, 2013, 6:22:49 PM1/16/13
to webd...@googlegroups.com
I had it all written out and Google lost it by forcing a page reload.

I would use XPath to find the folder name with something like this: "//th[contains(@id,'folder-name-')]"

You can get the number for the folder folder by doing a getAttribute("id" or "name") - whichever you want.  Then do a removeFirst() of the resulting string so you are left with just the number.

Use the number to find the div with the same number via XPath: "//div[@id='folder-table-" + folderNumber + "]"

By doing a findElement from that div you can get all the file names for that folder, or whatever else you may need.

Mike

Bhavani Sridharan

unread,
Jan 16, 2013, 7:32:44 PM1/16/13
to webd...@googlegroups.com
ok. Will try it out. thanks.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/F_eAHjCy4-sJ.
Reply all
Reply to author
Forward
0 new messages