我是硒新手,正在尝试使用LIST打印链接文本。
我正在尝试从一个演示站点获取文本,但是定位器上没有任何单个属性。所以请帮帮我。以下是我执行的详细信息。
网站网址:https://www.nopcommerce.com/
菜单:产品>产品下的所有子菜单(共找到9个链接),但无法打印这9个链接的文本。
截屏1: enter image description here
屏幕截图2: enter image description here
我完成的代码:
public void OpenStoreDemo() {
List<WebElement> MainMenu = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]//a"));
System.out.println(MainMenu.size());
for (WebElement list : MainMenu) {
String getname = list.getText();
System.out.println(getname);
if (getname.equals("Store demo")) {
System.out.println("Pass");
}
else {
System.out.println("Not Found");
}
break;
}
}
答案 0 :(得分:1)
以下代码可能会对您有所帮助。
Query query = session.createQuery(q.toString());
答案 1 :(得分:0)
尝试一下:
// to make 'Store demo' button visible, hover to 'product' button
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.xpath("//ul[@class = 'top-menu']/li[1]/a")));
actions.perform();
// wait until 'Store demo' button will be clickable
new WebDriverWait(webDriver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@class = 'top-menu']/li[1]/ul/li[4]/a")));
// locate element
WebElement storeDemoBtn = driver.findElement(By.xpath("//ul[@class = 'top-menu']/li[1]/ul/li[4]/a"));
System.out.println(storeDemoBtn.getText()); // will print 'Store demo'
答案 2 :(得分:0)
您可以使用 JS 方法从隐藏的Web元素获取文本,并使用 dataprovider 检查菜单项是否已启用,可能是这样的:
@DataProvider
public Object[][] getProductMenu() {
return new Object[][]{
{"What is nopCommerce"},
{"Why nopCommerce"},
{"Features"},
{"Store demo"},
{"Showcase"},
{"Case studies"},
{"Roadmap"},
{"Copyright notice removal"},
{"License"}
};
}
@Test(dataProvider = "getProductMenu")
public void checkProductMenu(String menuItemName) {
driver.get("https://www.nopcommerce.com/");
// check if menu item is enabled
Assert.assertTrue(isMenuItemExistInMenu(menuItemName));
}
public boolean isMenuItemExistInMenu(String menuItemName) {
List<WebElement> menuItemsElements = driver.findElements(By.xpath("//ul[@class='top-menu']/li[1]//ul/li/a"));
for (WebElement menuItemsElement : menuItemsElements) {
if (getHiddenTextByWebElement(menuItemsElement).equals(menuItemName)) {
System.out.println("Menu item [" + menuItemName + "]" + " is enabled in Product submenu");
return true;
}
}
return false;
}
//this is JS method for get hidden text from WebElement
private String getHiddenTextByWebElement(WebElement element) {
try {
return (String) driver.executeScript("return arguments[0].innerHTML", element);
} catch (Exception e) {
Assert.fail("Can't return hidden text, occur error: \n" + e.getMessage());
return "";
}
}