在尝试获取网页表的行数时获取维度

时间:2016-05-24 12:06:00

标签: selenium-webdriver

我尝试计算网页表的行数,但我得到了表的维度。  我有一个名为TableRow类的文件,这个类使用属性文件" webelement.properties" 。在属性文件中,我保留了web元素的路径

import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    public class TableRow {
        protected static WebDriver driver;
        @BeforeClass
        public static void setup() {

            driver = new FirefoxDriver();
            driver.get("some url");
            driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
            driver.manage().window().maximize();

        }
    public Properties getWebElementDetails() throws IOException {
        Properties p = new Properties();

            // Read object repository file
            InputStream stream = new FileInputStream(
                    new File(System.getProperty("user.dir") + "//src//com//properties//webelement.properties"));

            p.load(stream);
            return p;
        }


        public WebElement getElementByXPath(String Key) throws IOException {
            Properties propertiesValue = getWebElementDetails();
            try {

                return driver.findElement(By.xpath(propertiesValue.getProperty(Key)));
            } catch (Throwable t) {
                // If element not found on page then It will return null.

                return null;
            }
        }

        @Test()
        void testLogincase1() throws Exception{

            Thread.sleep(2000);

            System.out.println(getElementByXPath("tab")).getsize());

            }

    }

webelement.properties

tab=.//*[@id='LinkedForm_Organization_OfficeChild']/div[2]/div[1]/div/table/tbody/tr 

HTML COde。

<div class="row">
<div class="column">
<table class="linked-record-table data-table">
<thead>
<tr>
<th>Details</th>
<th>Name</th>
<th>Address 1</th>
<th>Address 2</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
</tr>
</thead>
<tbody>
<tr class="data-row" data-is-new="false">
<input id="Organization_OfficeChild_R1_" type="hidden" value="1" name="Organization_OfficeChild_R1_"/>
<input id="Organization_OfficeChild_R1__pky" type="hidden" value="4223" name="Organization_OfficeChild_R1__pky"/>
<td>
<a class="link darker" href="/Office/Details/4223?mode=Edit&_fcl=Organization&_fky=2291">
</td>
<td>
<td>
<td>
<td>
<td>
<td>
</tr>
<tr class="data-row" data-is-new="false">
<tr class="data-row new-row-template" data-is-new="true">
<tr class="data-row new-row-template" data-is-new="true">
<tr class="data-row new-row-template" data-is-new="true">
</tbody>
</table>
</div>

我做错了什么。请提供解决方案。

1 个答案:

答案 0 :(得分:0)

您好请确定表格的行和列,如下所示

 public Track[] findTracksByPlaylistId(Context context, Long id) {

    ContentResolver contentResolver = context.getContentResolver();
    String playListRef = "content://com.google.android.music.MusicContent/playlists/" + id + "/members";
    Uri songUri = Uri.parse(playListRef);
    Cursor cursor = contentResolver.query(songUri, PLAYLIST_COLUMNS, null, null, null);
    Track[] tracks = new Track[cursor.getCount()];

    int idColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members._ID);
    int artistColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.ARTIST);
    int titleColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.TITLE);
    int trackColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.TRACK);
    int durationColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.DURATION);
    int albumColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.ALBUM_ID);

    int index = 0;

    while (cursor.moveToNext()) {
        Track track = new Track(cursor.getInt(idColumn));
        track.setTitle(cursor.getString(titleColumn));
        track.setArtist(cursor.getString(artistColumn));
        track.setDuration(cursor.getInt(durationColumn));
        track.setTrackNumber(cursor.getInt(trackColumn));
        track.setAlbumId(cursor.getLong(albumColumn));
        Log.d(TAG, "ALBUM:" + track.getAlbumId());
        tracks[index++] = track;
    }

    cursor.close();
    return tracks;
}