我想从下拉菜单中选择一个值。我的代码只从下拉菜单中选择第一个值,我希望您单击下拉菜单中的所有值,并检查它们是否手动加载了正确的URL地址和标题选项卡。我没有看到任何错误,并告诉我测试通过了测试,但没有做我想要的。我使用PageObject和PageFactory。 请帮忙。
这是我从下拉菜单中获取第一个值的代码:
public void clickOnAccessories(){
WebElement element=driver.findElement(By.xpath("//a[text()='Product Category']"));
String mouseOver = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
((JavascriptExecutor)driver).executeScript(mouseOver, element);
waitForElementToBeDisplayed(driver.findElement(By.xpath("//*[text()='Accessories']")), 500);
Assert.assertTrue(driver.getCurrentUrl().equals("http://store.demoqa.com/products-page/product-category/accessories/"));
Assert.assertTrue(driver.getTitle().contains("Accessories"));
}
这是我的HTML代码:
<li id="menu-item-33" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-has-children menu-item-33 has_children">
<span class="before"> </span>
<a href="http://store.demoqa.com/products-page/product-category/">
<span></span>
Product Category
</a>
<ul class="sub-menu" style="display: none;">
<li id="menu-item-34" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-34">
<span class="before"> </span>
<a href="http://store.demoqa.com/products-page/product-category/accessories/" style="padding-left: 10px;">
<span></span>
Accessories
</a>
</li>
<li id="menu-item-35" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-35">
<span class="before"> </span>
<a href="http://store.demoqa.com/products-page/product-category/imacs/" style="padding-left: 10px;">
<span></span>
iMacs
</a>
</li>
答案 0 :(得分:0)
以下是在菜单上悬停鼠标然后单击子菜单链接的代码 -
Actions action = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[contains(.,'Product Category')]"));
Thread.sleep(2000);
action.moveToElement(menu).moveToElement(driver.findElement(By.xpath("//a[contains(.,'iMacs')]"))).click().build().perform();
如果要迭代菜单的所有子菜单,则必须获取字符串数组中的所有菜单名称,然后逐个传递xpath
中的名称
Actions action = new Actions(driver);
String[] submenus = {"Accessories", "iMacs", "iPads"};
for(int i=0;i<submenus.length;i++)
{
WebElement we = driver.findElement(By.xpath("//a[contains(.,'Product Category')]"));
Thread.sleep(2000);
action.moveToElement(we).moveToElement(driver.findElement(By.xpath("//a[contains(.,'"+submenus[i]+"')]"))).click().build().perform();
Thread.sleep(3000);
}