微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-如何等待,然后单击html表中的第一个链接

我刚刚开始玩硒游戏,正在寻找帮助.在我要测试的网页上,我有一个搜索按钮,当页面加载时,我有一个html表,其下方显示结果.

enter image description here

搜索结果表的html如下…

<table id="search-results-table" style="width: 1561px;">
    <thead>
    <tr role="row" style="height: 0px;">
        <th><div>Item Description</div></th>
        <th><div>Size</div></th>
        <th><div>Colour</div></th>
        <th><div>Supply Style</div></th>
        <th><div>Item #</div></th>
    </tr>
</thead>
<tbody>
    <tr role="row" class="odd">
        <td class="left  sorting_1 ">EASY STEPS TAC LILYPAD PUMP:GREEN:10</td>
        <td class=" center">10</td>
        <td class=" center">GREEN</td>
        <td class=" center">TAC</td>
        <td class=" center"><a href="javascript:setItemNumber(217592380);">217592380</a></td>
    </tr>
</tbody>
</table>

使用Selenium IDE,我能够弄清楚如何创建一个junit测试,它可以像屏幕快照中那样进行搜索,但是我正在努力找出如何修改单元测试以正确地等待搜索完成然后在页面上返回结果后,单击搜索结果表第一行中的第一项.

感谢有人可以帮忙.以下是到目前为止我尝试过的代码,但是没有为我的表返回任何td.对不起,我是一个真正的初学者.

public class MySearch {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();    
  @Before
  public void setUp() throws Exception {
    //driver = new FirefoxDriver();
    File file = new File("C:/selenium/IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    driver = new InternetExplorerDriver();  

    baseUrl = "http://myBaseUrl";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }
  @Test
  public void testSearch() throws Exception {    
    driver.get(baseUrl + "/wps/portal/mySearchUrl");      
    driver.findElement(By.id("keywords")).clear();
    driver.findElement(By.id("keywords")).sendKeys("BLACK SHOES");
    driver.findElement(By.id("searchButton")).click();
    navigatetoDetail();        
  }
  public void navigatetoDetail() throws Exception {
        //find tbody
        WebElement table = driver.findElement(By.id("search-results-table"));

        //get all rows
        List<WebElement> allRows = table.findElements(By.tagName("tr"));

        //iterate through the rows
        for (WebElement row : allRows) {
                //get the rowCells in each row
                List<WebElement> rowCells = row.findElements(By.tagName("td"));

                int indexofColumnwhichhasProjectname = 4;

                //get the column which contains the item no and get text
                String itemNo = rowCells.get(indexofColumnwhichhasProjectname).getText();

                System.out.println(itemNo);
        }
    } 

当通过输出进行调试时,我尝试获取td的那部分什么也不返回,因此当我尝试获取索引4时,它会返回arryIndexOutOfBounds.

List<WebElement> rowCells = row.findElements(By.tagName("td"));

@R_502_5620@:

感谢Alecxce在此方面的帮助.最后,我找到了一个可以解决问题的xpath表达式.

driver.findElement(By.xpath("//table[@id='search-results-table']/tbody/tr[‌​1]/td[5]/a")).click(); 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐