我试图在Selenium Java的新Tab中打开一个链接,但是第一次只有一个链接打开,但是当打开第二个链接时For Loop出错了,任何人都可以帮我解决这个问题。 / p>
这是我的代码。
public class Link_Open_In_New_Tab {
public WebDriver driver;
@BeforeTest
public void OpenBrowser() {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.nopcommerce.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void OpenLink() throws InterruptedException {
List<WebElement> ProMenu;
WebElement SubLinks;
driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/a")).click();
ProMenu = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]/ul[@class='sublist']/li/a"));
for (int i = 0; i < ProMenu.size(); i++) {
SubLinks = driver
.findElement(By.xpath("//ul[@class='top-menu']/li[" + (i + 1) + "]/ul[@class='sublist']/li/a"));
Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).click(SubLinks).keyUp(Keys.CONTROL).build().perform();
Thread.sleep(2000);
String winHandleBefore = driver.getWindowHandle();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
Thread.sleep(2000);
driver.close();
Thread.sleep(2000);
driver.switchTo().window(winHandleBefore);
Thread.sleep(2000);
//driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/a")).click();
//Thread.sleep(2000);
}
}
}
答案 0 :(得分:1)
您必须将鼠标悬停在产品上,才能获取所有sub menus items
。之后,您可以使用 Selenium 和 JAVA 中提供的 Actions 类来模拟键盘笔触。
您可以尝试以下代码:
public class Ashish {
static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\user***\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.nopcommerce.com/");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/a"))).build().perform();
List<WebElement> element = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]/ul[@class='sublist']/li/a"));
for(WebElement ele:element) {
action.keyDown(Keys.LEFT_CONTROL).moveToElement(ele).click().keyUp(Keys.LEFT_CONTROL).build().perform();
}
}
}
更新1 :
public class Ashish {
static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\user***\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.nopcommerce.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/a"))).build().perform();
List<WebElement> element = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]/ul[@class='sublist']/li/a"));
System.out.println(element.size());
for(int i = 0 ; i<element.size() ; i++) {
action.keyDown(Keys.LEFT_CONTROL).moveToElement(wait.until(ExpectedConditions.elementToBeClickable(element.get(i)))).click().keyUp(Keys.LEFT_CONTROL).build().perform();
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
System.out.println(driver.getTitle());
driver.close();
driver.switchTo().window(tabs.get(0));
}
}
}
控制台输出:
9
nopCommerce - ASP.NET free shopping cart solution. What is nopCommerce?
nopCommerce - ASP.NET Open-source Ecommerce Shopping Cart Solution
nopCommerce - ASP.NET open source eCommerce solution. Feature list.
nopCommerce - Shopping Cart Demo & Shopping Cart Solution
nopCommerce - open source shopping cart. Showcase. Live Shops.
nopCommerce - open source shopping cart. Case Studies and Success Stories.
nopCommerce - ASP.NET open source shopping cart. Roadmap.
nopCommerce copyright removal key - nopCommerce
The nopCommerce Public License Version 3.0 ("NPL") - nopCommerce
答案 1 :(得分:1)
您试图打开产品菜单中的所有子链接。但是您的子链接xpath指向所有菜单(li[" + (i + 1) + "]/ul[@class='sublist']/li/a
)的第一个子链接。因此,您需要按如下所示修改子链接xpath,然后尝试
SubLinks = driver.findElement(By.xpath("//ul[@class='top-menu']/li[1]/ul[@class='sublist']/li[" + (i + 1) + "]/a"));
答案 2 :(得分:0)
如果您打算测试链接标题是否按预期工作,那么为什么需要Crtl + click。
根据开发情况,此处的链接是单击而不是ctrl + click,我个人不愿意执行此操作。
创建变量,说int i = 1;
在循环中使用,请进行常规单击并验证标题。然后增加i并返回浏览器。