我正在尝试将鼠标悬停在“男性”类别标签上,并在此website上选择“衬衫”类别。但是,我无法点击Men项目中的“衬衫”类别,我也没有收到任何错误。这是我的代码:
public void PurchaseItemTest() throws InterruptedException, IOException {
Thread.sleep(3000);
//util.clickbyXpath(Constants.MENCATEGORYTAB);
WebElement element = util.getdriver().findElement(By.xpath("//a[@class='accord-header' ]"));
Actions action = new Actions(util.getdriver());
action.moveToElement(element).moveToElement(util.getdriver().findElement(By.xpath("//a[@class='accord-header' and contains(.,'Men')]"))).moveToElement(util.getdriver().findElement(By.xpath("//a[@title='Shirts']"))).click().build().perform();
答案 0 :(得分:1)
这有效:
final By DROPDOWN = By.cssSelector("li[class='atg_store_dropDownParent']");
final By DROPDOWN_LINK = By.cssSelector("a[class='accord-header ']");
List<WebElement> dropdowns = new WebDriverWait(util.getDriver(), 5)
.until(ExpectedConditions.presenceOfAllElementsLocatedBy(DROPDOWN));
WebElement men = dropdowns.stream()
.flatMap(dropdown -> dropdown.findElements(DROPDOWN_LINK).stream())
.filter(link -> link.getText().equals("MEN"))
.findFirst()
.orElse(null);
if(men != null) {
new WebDriverWait(util.getDriver(), 5)
.until(ExpectedConditions.elementToBeClickable(men));
Actions action = new Actions(util.getDriver());
action.moveToElement(men).build().perform();
new WebDriverWait(util.getDriver(), 5)
.until(ExpectedConditions.elementToBeClickable(SHIRTS))
.click();
}
你的男性类别标签的xpath对我来说很不错。当您悬停时,请确保在点击之前等待“Shirts”链接可点击。另外,请避免将Thread#sleep()
与Selenium一起使用。请改用Explicit Waits。