2013-04-22 77 views
0

下面是HTML:如何使用Selenium定位不穩定的表格元素?

<table class="wikitable openei-infobox"> 

    <tbody><tr> 
    <th> <a href="/wiki/Property:Name" title="Property:Name">Name</a> 
    </th> 
    <td> Ambit Energy, L.P. 
    <pre style="display: none;"></pre> 
    </td></tr> 
    <tr> 
    <th> <a href="/wiki/Property:Place" title="Property:Place">Place</a> 
    </th> 
    <td> <a href="/wiki/Maryland" title="Maryland">Maryland</a> 
    <pre style="display: none;"></pre> 
    ...... 

我需要找到使用硒元素馬里蘭,但我不能使用XPath如下("//a[@href='...']"),我不能用By.linkText要麼,因爲它不是穩定的。我試過以下內容:

String el = driver.findElement(By.xpath(("//table[@class='wikitable openei-infobox']/td[1]"))).get(1).getText(); 
     System.out.println(el); 

我的代碼有什麼問題?

回答

2

請嘗試以下操作: String el = driver.findElement(By.xpath((「// tr [2]/td [1]」)))。getText(); System.out.println(el);