使用 Selenium(Java),如何从 drop-down 菜单中 select 一个值
Using Selenium(Java), how to select a value from the drop-down menu
我想从 drop-down 菜单中 select 一个值。我的代码 select 只是 drop-down 菜单中的第一个值,我希望您单击 drop-down 菜单中的所有值并检查它们是否 hand-loaded正确的 URL 地址和标题选项卡。我没有看到任何错误,并向我显示测试通过了测试,但没有按照我的要求进行。我使用 PageObject 和 PageFactory。
请帮忙。
这是 drop-down 菜单中第一个值的代码:
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>
这是将鼠标悬停在菜单上然后单击子菜单的代码 link-
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();
如果你想遍历 Menu 的所有子菜单,那么要么你必须在字符串数组中获取所有菜单名称,然后将名称逐一传递到 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);
}
我想从 drop-down 菜单中 select 一个值。我的代码 select 只是 drop-down 菜单中的第一个值,我希望您单击 drop-down 菜单中的所有值并检查它们是否 hand-loaded正确的 URL 地址和标题选项卡。我没有看到任何错误,并向我显示测试通过了测试,但没有按照我的要求进行。我使用 PageObject 和 PageFactory。 请帮忙。
这是 drop-down 菜单中第一个值的代码:
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>
这是将鼠标悬停在菜单上然后单击子菜单的代码 link-
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();
如果你想遍历 Menu 的所有子菜单,那么要么你必须在字符串数组中获取所有菜单名称,然后将名称逐一传递到 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);
}