处理下拉列表中的子菜单项

问题描述 投票:0回答:1

我想从子菜单中找到元素

enter image description here

我试图找到印刷品,但找不到。我该如何解决这个问题?

我的代码

    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.flipkart.com");

    try {
        WebElement closeButton = driver.findElement(By.xpath("//button[contains(text(),'✕')]"));
        closeButton.click();
    } catch (Exception e) {
        System.out.println("No login popup to close.");
    }

    Actions actions = new Actions(driver);
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20)); // Increased wait time
    JavascriptExecutor js = (JavascriptExecutor) driver;

    try {

        WebElement computerPeripheralsMenu = wait.until(
                ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Computer Peripherals']/parent::div")));

        actions.moveToElement(computerPeripheralsMenu).perform();
        System.out.println("Hovered over 'Computer Peripherals' menu.");

        Thread.sleep(2000);

        System.out.println(driver.getPageSource());

        WebElement printersMenu = wait.until(
                ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Printers']")));


        js.executeScript("arguments[0].scrollIntoView(true);", printersMenu);


        printersMenu.click();
        System.out.println("Clicked on 'Printers' submenu item.");


        System.out.println("Current URL: " + driver.getCurrentUrl());

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // Close the browser
        driver.quit();
    }
}
java selenium-webdriver xpath allocator
1个回答
0
投票

首先,当我查看您的代码时,您没有将鼠标悬停在 Flipkart 主页上的“电子产品”选项卡上。将鼠标悬停在“仅电子设备”上后,您将能够找到“计算机外围设备”,然后是“打印机”。

请在下面找到适合您的场景的工作代码,

WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize(); // It is always better to maximize screen to avoid to prevent the missing of few web elements
    driver.get("https://www.flipkart.com");

    try {
        WebElement closeButton = driver.findElement(By.xpath("//button[contains(text(),'✕')]"));
        closeButton.click();
    } catch (Exception e) {
        System.out.println("No login popup to close.");
    }
    Actions actions = new Actions(driver);
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20)); // Increased wait time
    JavascriptExecutor js = (JavascriptExecutor) driver;
    try {
        WebElement electronics = driver.findElement(By.xpath("//div[@aria-label='Electronics']")); //You need to hover over Electronics first
        actions.moveToElement(electronics).perform();
        WebElement computerPeripheralsMenu = driver.findElement(By.xpath("//a[text()='Computer Peripherals']")); //The xpath for Computer peripherals was incorrect
        actions.moveToElement(computerPeripheralsMenu).perform();
        System.out.println("Hovered over 'Computer Peripherals' menu.");
        Thread.sleep(2000);
        //System.out.println(driver.getPageSource()); //I did not understand the significance of this step
        WebElement printersMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Printers']")));
        js.executeScript("arguments[0].scrollIntoView(true);", printersMenu);
        printersMenu.click();
        System.out.println("Clicked on 'Printers' submenu item.");
        System.out.println("Current URL: " + driver.getCurrentUrl());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // Close the browser
        driver.quit();
    }
© www.soinside.com 2019 - 2024. All rights reserved.