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

搜索结果表的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"));发布于 2015-07-28 12:30:37
感谢Alecxce在这方面的帮助。最后,我找到了一个xpath表达式,它完成了这个任务。
driver.findElement(By.xpath("//table[@id='search-results-table']/tbody/tr[1]/td[5]/a")).click(); 发布于 2015-07-27 12:03:51
显式等待搜索结果可见:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("search-results-table")));https://stackoverflow.com/questions/31644750
复制相似问题