【发布时间】:2016-09-19 12:04:34
【问题描述】:
目标:脚本逐个点击子链接并验证页面是否存在
场景: 当我将鼠标悬停在类别链接上时 - 它会在右侧显示子类别链接(列表):
当我收回悬停时 - 链接(列表)不会显示:
自动化: 我试图将鼠标悬停在类别链接上!! 从而捕获了Category链接的Xpath!! 鼠标悬停的应用动作!! 鼠标悬停在 0 索引(开始)处捕获子链接的 x 路径! 应用“For Each loop”(高级For循环)来一一捕获子链接x路径!! 在上面的for循环中,使用“action”点击子链接!!
问题:它点击索引 0 处的子链接,在以后的迭代中,即使鼠标悬停在类别链接上,它也无法捕捉到子链接...
如何在第二,第三......迭代......
以下是代码:
public static WebElement Add_EditCorpUser(WebDriver driver) throws InterruptedException
{
driver.switchTo().defaultContent();
driver.switchTo().frame(1);
Actions action = new Actions(driver);
element=driver.findElement(By.xpath(".//td[@title='Security Admin']/following::td[1]/span[contains(text(),'Security')]"));
action.moveToElement(element).build().perform();
List<WebElement> arrayList=driver.findElements(By.xpath(".//table[@class='hideHeader']//span[@id='subMenu']//div[@id='i_2sub']//tbody/tr"));
List<String> linksname = new ArrayList<>();
for (WebElement w:arrayList) {
linksname.add(w.getText());
System.out.println("value ---------------- --"+ linksname);
action.moveToElement(w).click().build().perform();
Thread.sleep(10000);
driver.switchTo().defaultContent();
driver.switchTo().frame(1);
action.moveToElement(driver.findElement(By.xpath(".//td[@title='Security Admin']/following::td[1]/span[contains(text(),'Security')]"))).build().perform();
Thread.sleep(10000);
}
----------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------根据注释修改代码------------- ---------
public static WebElement element=null;
public static WebElement Add_EditCorpUser(WebDriver driver) throws InterruptedException
{
driver.switchTo().defaultContent();
driver.switchTo().frame(1);
Actions action = new Actions(driver);
element=driver.findElement(By.xpath(".//td[@title='Security Admin']/following::td[1]/span[contains(text(),'Security')]"));
action.moveToElement(element).build().perform();
List<WebElement> arrayList=driver.findElements(By.xpath(".//table[@class='hideHeader']//span[@id='subMenu']//div[@id='i_2sub']//tbody/tr"));
String[] xpa=new String[arrayList.size()];
for(int i=1;i<arrayList.size();i++)
{
xpa[i]="\".//table[@class='hideHeader']//span[@id='subMenu']//div[@id='i_2sub']//tbody/tr["+i+"]\"";
}
for(String a: xpa )
{
if(a!=null){
System.out.println(a);
action.moveToElement(driver.findElement(By.xpath(a))).click().build().perform();
Thread.sleep(10000);
driver.switchTo().defaultContent();
driver.switchTo().frame(1);
System.out.println("Switched to new frame");
action.moveToElement(element).build().perform();
}
【问题讨论】:
标签: selenium selenium-webdriver automation testng selenium-grid