我是Selenium webdriver的新手,并且学习了动态表,因为这一刻我陷入困境。我想在动力学表中点击特定的公司名称,我已经为它编写了示例脚本,请告诉我它有什么问题。
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.icicidirect.com");
Thread.sleep(1000);
driver.findElement(By.xpath("//a[contains(text(),'Markets')]")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//a[contains(text(),'Daily Share Prices')]")).click();
Thread.sleep(3000);
TablePageObject tablePageObject = PageFactory.initElements(driver, TablePageObject.class);
tablePageObject.clickLink("ABB");
}
}

public class TablePageObject {
private WebDriver driver;
@
FindBy(css = "table tr")
private List < WebElement > allTableRows; // find all the rows of the table
public TablePageObject(WebDriver driver) {
this.driver = driver;
}
public void clickLink(String SecurityName) {
for (WebElement row: allTableRows) {
List < WebElement > links = row.findElements(By.linkText("ABB"));
// the first link by row is the company name, the second is link to be clicked
if (links.get(0).getText().contains(SecurityName)) {
links.get(0).click();
}
}
}
}
&#13;
答案 0 :(得分:0)
几点建议:
您可以使用以下链接直接接收所需的表格
driver.get(&#34; http://content.icicidirect.com/newsiteContent/Market/MarketStats.asp?stats=DailySharePrices&#34);
您可以等待表格加载
然后你会找到该元素并点击它(就像在你的代码中一样)。
这是适用于我的代码
WebDriver driver = new FirefoxDriver();
try{
driver.get("http://content.icicidirect.com/newsiteContent/Market/MarketStats.asp?stats=DailySharePrices");
(new WebDriverWait(driver, 10/*sec*/)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("ABB")));
List<WebElement> dailyList = driver.findElements(By.linkText("ABB"));
if (dailyList.size()!=0) {
dailyList.get(0).click();
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
driver.close();
}