簡體   English   中英

如何克服 state 元素參考異常

[英]How to overcome state Element Reference exception

我正在嘗試自動化以下操作:

  1. 在 Electronics 上啟動https://www.flipkart.com > 點擊手機 > 鼠標 hover 然后點擊 Mi。

我在線程“main”state 元素參考中得到預期:元素未附加到 miButton() 方法中的頁面文檔。

請參閱錯誤詳細信息部分。

HTML 代碼

小米按鈕點擊 - HTML

底座 class:

public class Base {
    
        static WebDriver driver;
        
        
    public void setupBrowser(String browser, String url) {
        String currDir = System.getProperty("user.dir");
        
        if(browser.equalsIgnoreCase("chrome")) {
            System.setProperty("webdriver.chrome.driver", currDir + "\\drivers\\chromedriver.exe");
            driver = new ChromeDriver();
        }
        else if(browser.equalsIgnoreCase("firefox")) {
            System.setProperty("webdriver.gecko.driver", currDir + "\\drivers\\geckodriver.exe");
            driver = new FirefoxDriver();
        }
        else if(browser.equalsIgnoreCase("edge")) {
            System.setProperty("webdriver.edge.driver", currDir + "\\drivers\\msedgedriver.exe");
            driver = new EdgeDriver();
        }
        else {
            System.out.println("Valid browser not found therefore quitiing");
            System.exit(0);
        }
        
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        if(url != "") 
            driver.get(url);
        else
            driver.get("about:blank");          
    }
    
    public void closeBrowser() {
        driver.close();
    }

    

頁 Class

    public class pagetest extends Base{
    
        Actions action;
    
        public void closebtn() {
            driver.findElement(By.cssSelector("button._2doB4z")).click();  
        }
    
        public void mibutton() {
            WebElement mobiles = driver.findElement(By.xpath("//div[text()='Mobiles']"));  
    
            action = new Actions(driver);
            action.moveToElement(mobiles).click().perform();
    
    
            WebElement electronicsmenu = driver.findElement(By.xpath("//span[text()='Electronics']"));
            action.moveToElement(electronicsmenu).click().perform();
    
            List <WebElement> value = driver.findElements(By.xpath("//div[@class='_1kidPb']/div[@class='_1QrT3s']//a");
            for(WebElement elem:value) {
                if(elem.getText().equals("Mi")) {
                    elem.click();
                }
            }
    
            WebElement label = driver.findElement(By.xpath("//p[text()='Latest from MI : ']"));
            System.out.println("The Label 'Latest from MI' is present : " +label.isEnabled());
        }
    
    public static void main(String[] args) {
            pagetest obj = new pagetest();
            obj.setupBrowser("chrome", "https://www.flipkart.com/");
            obj.closebtn();
            obj.mibutton();
        }
    
    }

錯誤詳情

線程“main”中的異常 org.openqa.selenium.StaleElementReferenceException:過時的元素引用:元素未附加到頁面文檔(會話信息:chrome=89.0.4389.90)有關此錯誤的文檔,請訪問: Z5E056C500A1C4B6A7110B50D807BADE5 .seleniumhq.org/exceptions/stale_element_reference.html

您永遠不會找到元素,然后立即嘗試單擊它們。 當頁面加載時,元素被創建,但在幾毫秒后它們會改變它們的屬性、大小和/或位置。 此異常意味着您找到了該元素,但到目前為止您“丟失”了它。

為了避免 StateElementReferecneException 使用 WebDriverWait object。 您可以等待元素可見/可點擊以及其他選項。 最好的選擇是等待元素可點擊:

  WebDriverWait wait = new WebDriverWait(driver, 20, 10);        
  wait.until(ExpectedConditions.elementToBeClickable
  (By.id("elementID")));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM