java - 陈旧元素引用 : element is not attached to the page document for Java-Selenium

标签 java selenium staleelementreferenceexception

我想做的是在登录后,驱动程序将单击“属性”下拉菜单,选择一个选项并单击“提交”并重复该过程,直到循环完成。

下面是我的代码:

package com.genericlibrary;

import java.util.List;

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.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.util.Highlighter;

public class MobopsSearchJobsFromDropDown {

    WebDriver driver;
    Highlighter color;

    public void getSetup() {
        String path = System.getProperty("user.dir");
        String driverPath = path + "\\Driver\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", driverPath);
        driver = new ChromeDriver();
        driver.navigate().to("http://mobops-test.jcdecauxna.com/");
        driver.manage().window().maximize();
    }

    public void logIntoMobops() {
        WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
        WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
        WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));

        userName.sendKeys("test2");
        passWord.sendKeys("1234");
        loginButton.click();

    }

    public void selectEachPropertyAndSeachJob() {
        WebElement dateRange = driver.findElement(By.xpath("//*[contains(@name,'date_range')]"));
        WebElement last7days = driver.findElement(By.xpath("(//*[contains(text(),'Last 7 Days')])[2]"));
        WebElement searchJobs = driver.findElement(By.xpath("//*[contains(@name,'layout')]"));
        WebElement propertyDropdown = driver.findElement(By.xpath("//*[contains(@id,'property_id')]"));

        Select dropdown = new Select(propertyDropdown);

        List<WebElement> optionsInPropertyDropdown = dropdown.getOptions();
        for (int i = 0; i < optionsInPropertyDropdown.size(); i++) {
            if (propertyDropdown.isDisplayed() && propertyDropdown.isEnabled()) {
                try {
                    propertyDropdown.click();
                    dropdown.selectByVisibleText(optionsInPropertyDropdown.get(i).getText());
                    dateRange.click();
                    last7days.click();
                    searchJobs.click();
                    System.out.println("Option Search is " + optionsInPropertyDropdown.get(i).getText());

                } catch (org.openqa.selenium.StaleElementReferenceException ex) {
                    WebDriverWait wait = new WebDriverWait(driver, 30);
                    wait.until(ExpectedConditions.visibilityOf(propertyDropdown));
                }
            }

        }

    }

    public static void main(String[] args) {

        MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
        obj.getSetup();
        obj.logIntoMobops();
        obj.selectEachPropertyAndSeachJob();

    }
}

现在驱动程序只需选择第一个选项并单击提交。搜索完成后页面加载后立即出现以下错误:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

我曾尝试在我的代码中实现以下代码,但由于我是新手,我不知道如何实现以下代码来解决问题:

new WebDriverWait(driver, timeout)
        .ignoring(StaleElementReferenceException.class)
        .until((WebDriver d) -> {
            d.findElement(By.xpath("//*[contains(@id,'property_id')]")).click();
            return true;
        });

非常感谢任何解决此问题的帮助。谢谢。

最佳答案

有人帮我解决了这个问题。下面是解决方案:

public class MobopsSearchJobsFromDropDown {
    WebDriver driver;
    HighLighter color;

    public void getSetup() throws Throwable {
        String path = System.getProperty("user.dir");
        String driverPath = path + "\\driver\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", driverPath);
        driver = new ChromeDriver();
        driver.navigate().to("http://mobops-test.jcdecauxna.com/");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }

    public void logIntoMobops() throws Throwable  {
        WebElement userName = driver.findElement(By.xpath("//*[contains(@id,'username')]"));
        WebElement passWord = driver.findElement(By.xpath("//*[contains(@id,'password')]"));
        WebElement loginButton = driver.findElement(By.xpath("//*[contains(text(),'Login')]"));

        userName.sendKeys("test2");
        passWord.sendKeys("1234");
        loginButton.click();

    }

    public void selectEachPropertyAndSeachJob() throws Throwable  {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        List<WebElement> dropdownoptions = driver.findElements(By.xpath("//select[@id = 'property_id']//option"));
        for(int i =0; i<dropdownoptions.size(); i++) {
            String propertyDropdown = "//*[contains(@id,'property_id')]";
            String dateRange = "//*[contains(@name,'date_range')]";
            String last7days = "(//*[contains(text(),'Last 7 Days')])[2]";
            String searchJobs = "//*[contains(@name,'layout')]";
            Select dropdown = new Select(waitMethod(propertyDropdown));
            WebElement option = dropdown.getOptions().get(i);
            wait.until(ExpectedConditions.not(ExpectedConditions.stalenessOf(option)));
            dropdown.selectByVisibleText(option.getText());
            System.out.println(option.getText());
            waitMethod(dateRange).click();
            waitMethod(last7days).click();
            waitMethod(searchJobs).click();
            driver.navigate().refresh();    
        }
    }
    public WebElement waitMethod(String waiting) {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        WebElement waitForRefresh =wait.until(ExpectedConditions.refreshed(ExpectedConditions.presenceOfElementLocated(By.xpath(waiting))));
        return waitForRefresh;
    }

    public static void main(String[] args) throws Throwable {
        MobopsSearchJobsFromDropDown obj = new MobopsSearchJobsFromDropDown();
        obj.getSetup();
        obj.logIntoMobops();
        obj.selectEachPropertyAndSeachJob();
    }

}

关于java - 陈旧元素引用 : element is not attached to the page document for Java-Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53398835/

相关文章:

java - selenium.click() 不起作用 : Argument 1 of EventTarget. dispatchEvent 没有实现接口(interface)事件

java - 如何使用 Selenium Webdriver 查找网页上多个按钮的数量

java - 如何通过 Selenium 通过 XPath 访问 WebElement?

python - 循环元素时如何解决 StaleElementReferenceException (Selenium)

java - 使用 Picasso 加载图像,在角落显示颜色

Java 高级成像 : How to get the ImageLayout from a huge image?

java - List.of() 或 Collections.emptyList()

python - 陈旧元素引用 : element is not attached to the page document with the chrome web driver

selenium - PageFactory 中的 StaleElementReference 异常

java - Hibernate 无法将许多学生映射到一个类(class)