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

动态下拉框抛出Javascript error: circular reference错误的原因排查

解决Selenium操作SpiceJet出发地选择时的「circular reference」错误

嘿,我来帮你搞定这个问题!先看你的代码,有几个明显的问题触发了这个JS循环引用错误,咱们一步步来修复:

错误原因分析

首先你的XPath语法写错了:\\a[@value='DEL' 这里有两个问题——开头是双反斜杠(应该是双正斜杠//),而且结尾少了闭合的括号]。这种无效的XPath会让Selenium在和浏览器JS交互时触发异常,也就是你看到的「circular reference」错误。另外,SpiceJet的页面元素是动态加载的,直接点击可能赶不上元素就绪的时机,也会加重这类JS异常。

修复步骤

  1. 修正XPath语法
    把错误的定位代码改成正确的XPath:

    driver.findElement(By.xpath("//a[@value='DEL']")).click();
    
  2. 添加显式等待(推荐)
    为了避免动态元素未就绪的问题,最好用显式等待确保元素可点击后再操作:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@value='DEL']"))).click();
    

    这样能保证代码在元素完全加载好之后再执行点击操作,减少JS层面的异常。

  3. 备选定位方案(如果XPath还是有问题)
    有时候页面元素的value属性可能有变化,你也可以通过文本内容定位:

    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[text()='Delhi (DEL)']"))).click();
    

完整修正后的代码

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;

public class DynamicDropDown { 
    public static void main(String[] args) { 
        System.setProperty("webdriver.chrome.driver", "D:\\Chrome Driver 76.0\\chromedriver_win32\\chromedriver.exe"); 
        WebDriver driver = new ChromeDriver(); 
        driver.get("http://spicejet.com"); 
        
        // 等待出发地输入框可点击并点击
        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement originInput = wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_originStation1_CTXT")));
        originInput.click();
        
        // 等待DEL选项可点击并选择
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@value='DEL']"))).click();
    } 
}

额外提示

  • 确保你的ChromeDriver版本和Chrome浏览器版本匹配(你用的是76.0的Driver,对应的Chrome也应该是76左右的版本,版本不匹配也可能导致奇怪的JS异常)。
  • 如果还是遇到问题,可以尝试用Actions类模拟点击,或者检查页面是否有嵌套iframe(不过SpiceJet这个页面应该没有)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:56:04