首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何等待,然后点击html表格中的第一个链接

如何等待,然后点击html表格中的第一个链接
EN

Stack Overflow用户
提问于 2015-07-27 11:56:38
回答 2查看 2.8K关注 0票数 3

我刚刚开始使用selenium,我正在寻求一些帮助。在我正在尝试测试的网页上,我有一个搜索按钮,当页面加载时,我有一个html表格显示在它下面的结果。

搜索结果表的html如下所示...

代码语言:javascript
复制
<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。对不起,我是一个真正的初学者。

代码语言:javascript
复制
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。

代码语言:javascript
复制
List<WebElement> rowCells = row.findElements(By.tagName("td"));
EN

回答 2

Stack Overflow用户

发布于 2015-07-28 12:30:37

感谢Alecxce在这方面的帮助。最后,我找到了一个xpath表达式,它完成了这个任务。

代码语言:javascript
复制
driver.findElement(By.xpath("//table[@id='search-results-table']/tbody/tr[‌​1]/td[5]/a")).click(); 
票数 3
EN

Stack Overflow用户

发布于 2015-07-27 12:03:51

显式等待搜索结果可见:

代码语言:javascript
复制
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("search-results-table")));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31644750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档