gpt4 book ai didi

java - 尝试将鼠标悬停在选项卡上并对 Selenium 中的特定项目执行自动单击

转载 作者:行者123 更新时间:2023-12-02 11:16:04 25 4
gpt4 key购买 nike

我正在尝试将鼠标悬停在“男士”类别选项卡上,然后选择此 website 上的“衬衫”类别。但是,我无法单击男士项目中的“衬衫”类别,我也没有收到任何错误。这是我的代码:

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();

最佳答案

这有效:

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 对我来说表现得很奇怪。当您将鼠标悬停时,请确保等待“衬衫”链接可单击后再单击。另外,避免将 Thread#sleep() 与 Selenium 一起使用。使用Explicit Waits相反。

关于java - 尝试将鼠标悬停在选项卡上并对 Selenium 中的特定项目执行自动单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50279204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com