Java 列表<;WebElement>;给出空白结果
我正在为一些网站创建一个自动测试,我正在使用用Java编写的WebDriver和代码。我在这里面临两个问题Java 列表<;WebElement>;给出空白结果,java,arraylist,selenium-webdriver,Java,Arraylist,Selenium Webdriver,我正在为一些网站创建一个自动测试,我正在使用用Java编写的WebDriver和代码。我在这里面临两个问题 click()方法不起作用 当我试图阅读WebElement列表时,它显示为空白。但它显示的是正确的size() 我的代码: System.setProperty("webdriver.gecko.driver", "D:\\back up\\Proj\\selenium\\Jars\\Drivers\\geckodriver.exe"); WebDriver driver=new Fir
click()
方法不起作用size()
System.setProperty("webdriver.gecko.driver", "D:\\back up\\Proj\\selenium\\Jars\\Drivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
List<WebElement> allBrands = driver.findElements(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/ul/li/a"));
List<WebElement> allBrandsInShowAll = driver.findElements(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[2]/ul/li/a"));
System.out.println("Total No of Brands "+(allBrands.size() + allBrandsInShowAll.size()));
driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[1]/a")).click(); // This is not clicking on ShowAll Button
for(WebElement ele :allBrandsInShowAll) {
System.out.println("Name=>"+ele.getText());
}
for(WebElement ele :allBrands) {
System.out.println("Name=>"+ele.getText()); // Not printing the names of Brands
}
System.setProperty(“webdriver.gecko.driver”,“D:\\backup\\Proj\\selenium\\Jars\\Drivers\\geckodriver.exe”);
WebDriver=newfirefoxdriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
驱动程序。获取(“http://www.americangolf.co.uk/golf-clubs/fairway-woods");
列出allBrands=driver.findElements(By.xpath(“/*[@id='secondary']]/div[1]/div[2]/div/ul/li/a”);
列出allBrandsInShowAll=driver.findElements(By.xpath(“/*[@id='secondary']]/div[1]/div[2]/div/div[2]/ul/li/a”);
System.out.println(“品牌总数”+(allBrands.size()+allBrandsInShowAll.size());
driver.findelelement(By.xpath(“//*[@id='secondary']]/div[1]/div[2]/div/div/div[1]/a”)。单击()这不是单击ShowAll按钮
for(WebElement ele:allBrandsInShowAll){
System.out.println(“Name=>”+ele.getText());
}
对于(WebElement ele:allBrands){
System.out.println(“Name=>”+ele.getText());//不打印品牌名称
}
div.showall
从品牌选项中单击showall按钮即可
driver.findElement(By.cssSelector("div.showall")).click();
driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[1]/a")).click()
getText()
的父元素不正确
List<WebElement> allBrands = driver.findElements(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/ul/li/a"));
将输出打印为
Name=>CobraGolf
(14)
Name=>CallawayGolf
(10)
Name=>TaylorMade
(八)
编辑:OP提到,单击
ShowAll
按钮后,他无法获得其他品牌选项。原因是它们位于另一个子系统中
请将您的最终代码修改为此-
//clicks on ShowAll button
driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[1]/a")).click();
Thread.sleep(5000);
//gets parent for Categories
WebElement parentElement = driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/ul"));
//gets parent for Categories shown after clicking ShowAll
WebElement secondParentElement = driver.findElement(By.xpath(".//*[@id='secondary']/div[1]/div[2]/div/div[2]/ul"));
List<WebElement> allBrands = parentElement.findElements(By.cssSelector("li >a"));
List<WebElement> allBrandsInShowAll =secondParentElement.findElements(By.cssSelector("li >a"));
System.out.println("Total No of Brands "+(allBrands.size() + allBrandsInShowAll.size()));
for(WebElement ele :allBrands) {
System.out.println("Name=>"+ele.getText());
}
for(WebElement ele :allBrandsInShowAll) {
System.out.println("Name=>"+ele.getText());
}
driver.quit();
}
}
//单击ShowAll按钮
findElement(By.xpath(“//*[@id='secondary']/div[1]/div[2]/div/div[1]/a”)。单击();
睡眠(5000);
//获取类别的父级
WebElement parentElement=driver.findElement(By.xpath(“/*[@id='secondary']]/div[1]/div[2]/div/ul”);
//获取单击ShowAll后显示的类别的父级
WebElement secondParentElement=driver.findElement(By.xpath(“./*[@id='secondary']]/div[1]/div[2]/div/div[2]/ul”);
列出allBrands=parentElement.findElements(By.cssSelector(“li>a”));
List allBrandsInShowAll=secondParentElement.findElements(By.cssSelector(“li>a”));
System.out.println(“品牌总数”+(allBrands.size()+allBrandsInShowAll.size());
对于(WebElement ele:allBrands){
System.out.println(“Name=>”+ele.getText());
}
for(WebElement ele:allBrandsInShowAll){
System.out.println(“Name=>”+ele.getText());
}
driver.quit();
}
}
这将打印品牌内部的所有类别-
品牌总数13
- Name=>CobraGolf (14)
- Name=>CallawayGolf (10)
- Name=>TaylorMade (八)
- Name=>Ping (六)
- Name=>Benross (四)
- Name=>NikeGolf (二)
- Name=>Titleist (二)
- Name=>Wilson (二)
- Name=>WilsonStaff (二)
- Name=>Fazer (一)
- Name=>MizunoGolf (一)
- Name=>USKidsGolf (一)
- Name=>WilsonDeepRed (一)
//clicks on ShowAll button
driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[1]/a")).click();
Thread.sleep(5000);
//gets parent for Categories
WebElement parentElement = driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/ul"));
//gets parent for Categories shown after clicking ShowAll
WebElement secondParentElement = driver.findElement(By.xpath(".//*[@id='secondary']/div[1]/div[2]/div/div[2]/ul"));
List<WebElement> allBrands = parentElement.findElements(By.cssSelector("li >a"));
List<WebElement> allBrandsInShowAll =secondParentElement.findElements(By.cssSelector("li >a"));
System.out.println("Total No of Brands "+(allBrands.size() + allBrandsInShowAll.size()));
for(WebElement ele :allBrands) {
System.out.println("Name=>"+ele.getText());
}
for(WebElement ele :allBrandsInShowAll) {
System.out.println("Name=>"+ele.getText());
}
driver.quit();
}
}