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

测试服务器上Selenium Java自动化登录后无法定位元素求助

React网站Dashboard页面Selenium元素定位失败问题

我在公司测试服务器上进行网站自动化测试,登录页面可通过账号、密码及OTP正常完成自动化操作,登录后在同一窗口打开Dashboard页面,但在此页面中使用Selenium始终无法定位任何元素,频繁出现NoSuchElementException异常。

已尝试方案

  • 咨询开发得知网站为React Web App,尝试处理AJAX请求等操作,无效。
  • 尝试使用Selenium的Implicit Wait、Explicit Wait及Thread.sleep,无效。
  • 检查是否存在iFrame或需要窗口切换,未发现相关情况;通过断言验证Dashboard页面标题成功,确认Selenium已处于目标页面。
  • 开发建议尝试存储token,推测存在token问题,但我对此不理解具体含义。

测试代码

public class Test1 {
    WebDriverWait wait = null;
    WebDriver driver = null;
    WebElement searchField = null;
    @Parameters("browser")
    @BeforeClass 
    public void beforeclass(@Optional("chrome") String browser){
        System.setProperty("webdriver.chrome.driver","./Drivers/chromedriver.exe");
        driver = new ChromeDriver();        
        driver.get("WEBSITE");
        wait = new WebDriverWait(driver, Duration.ofSeconds(25));
    } 
        
    @Test
        public void login() throws InterruptedException  {
            driver.manage().window().maximize();
            WebElement searchField = driver.findElement(By.xpath("//input[@class='sc-kDTinF hjZgsN']"));
            searchField.sendKeys("MYUSERID");
            WebElement searchFiel = driver.findElement(By.xpath("//input[@type='password']"));
            searchFiel.sendKeys("MYPASS");
            WebElement searchbtn = driver.findElement(By.xpath("//button[@type='button']")); 
            searchbtn.click();
            WebElement srch = driver.findElement(By.xpath("//input[@aria-label='Please enter verification code. Digit 1'][@autocomplete='off']"));  
            srch.sendKeys("1"); 
            WebElement search = driver.findElement(By.xpath("//input[@aria-label='Digit 2']"));
            search.sendKeys("2");
            WebElement sarch = driver.findElement(By.xpath("//input[@aria-label='Digit 3']"));
            sarch.sendKeys("3");
            WebElement sch = driver.findElement(By.xpath("//input[@aria-label='Digit 4']"));
            sch.sendKeys("4");
            WebElement serchbtn = driver.findElement(By.xpath("//button[@type='button']"));   
            serchbtn.click();
    }
    @Test 
        public void calendarSetting() {
            String actualtitle = driver.getTitle();
            Assert.assertEquals(actualtitle, "SafetyConnect");          
            wait.until(ExpectedConditions.VisibilityOfElementLocated(By.className("sc-hiCibw")));
            WebElement ele = driver.findElement(By.className("sc-hiCibw"));
            ele.click();
    }
}

相关文件说明

包含Dashboard页面的HTML代码及NoSuchElementException异常信息文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 06:52:45