You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium Java操作Saucedemo时遇element not interactable错误求助

Selenium操作Saucedemo汉堡菜单报错解决

问题描述

使用Selenium Java编写代码操作Saucedemo网站时,点击汉堡菜单后选择子菜单选项,出现以下错误:

element not interactable: https://saucelabs.com/ has no size and location

尝试过解决方法但无效,预期代码能打开汉堡菜单并点击子菜单选项。

报错代码

package codeclass;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class assign1code {
    public static void main(String[] args){
        System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.saucedemo.com/");
        driver.manage().window().maximize();
        
        WebElement email=driver.findElement(By.id("user-name"));
        email.sendKeys("standard_user");
        WebElement password=driver.findElement(By.xpath("//*[@id=\"password\"]"));
        password.sendKeys("secret_sauce");
        driver.findElement(By.id("login-button")).click();
        //identify menu

        WebElement n=driver.findElement(By.xpath("/html/body/div/div/div/div[1]/div[1]/div[1]/div/div[1]/div/button"));
        // object of Actions with method moveToElement

        Actions a=new Actions(driver);

        a.moveToElement(n).perform();
        //identify sub-menu element

        WebElement m= driver.findElement(By.xpath("/html/body/div/div/div/div[1]/div[1]/div[1]/div/div[2]/div[1]/nav/a[2]"));
        a.moveToElement(m).click().perform();

        //driver.quit();

    }
}

问题分析与修复方案

问题根源

  1. Saucedemo的汉堡菜单需要点击触发展开,而非鼠标hover,原代码用moveToElement无法正确展开菜单
  2. 使用了绝对XPATH,页面结构微小变化就会导致定位失败
  3. 未等待菜单展开就直接定位子元素,此时子元素处于不可见/不可交互状态,报错"has no size and location"

修复后的代码

package codeclass;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class assign1code {
    public static void main(String[] args){
        System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.saucedemo.com/");
        driver.manage().window().maximize();
        
        // 登录操作
        WebElement email = driver.findElement(By.id("user-name"));
        email.sendKeys("standard_user");
        WebElement password = driver.findElement(By.id("password")); // 替换为更稳定的ID定位
        password.sendKeys("secret_sauce");
        driver.findElement(By.id("login-button")).click();
        
        // 点击汉堡菜单(使用稳定的ID定位)
        WebElement hamburgerMenu = driver.findElement(By.id("react-burger-menu-btn"));
        hamburgerMenu.click();
        
        // 显式等待子菜单元素可交互(以"About"选项为例,对应原代码的a[2])
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement aboutOption = wait.until(ExpectedConditions.elementToBeClickable(By.id("about_sidebar_link")));
        
        // 点击子菜单选项
        aboutOption.click();

        // driver.quit();
    }
}

关键改进点

  • 用元素的id替换绝对XPATH,提升定位稳定性
  • 直接点击汉堡菜单触发展开,而非hover
  • 使用WebDriverWait显式等待子菜单元素可交互,确保元素处于可操作状态

内容的提问来源于stack exchange,提问作者kiran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 04:28:13