0

I was with a element is not attached to the page document in my Java script for automation using SeleniumWebDriver.

I searched the internet and found the script below that solved the error element is not attached to the page document.

boolean breakIt = true;

    while (true) {

        breakIt = true;

        try {

            Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
            acao.selectByValue("519");

        } catch (Exception e) {

            if (e.getMessage().contains("element is not attached")) {

                breakIt = false;
            }
        }
    }

I continued the automation process and right after the above script, I added the code below to select an option within a DropDown List.

Select status = new Select(driver.findElement(By.id("cboStatus")));

At this point Eclipse displays the error message: Unreachable code.

I searched the internet but did not find anything about the error message regarding SeleniumWebDriver.

Here is the complete code and error message displayed by Eclipse.

public class validarStatus {

static WebDriver driver;

@Before
public void setUp() throws Exception {

    System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");

}

@Test
public void logarBkoMais() throws InterruptedException {

    WebDriver driver = new ChromeDriver();
    driver.get("http://10.5.9.45/BKOMais_S86825EstrategiaBackOfficeClaroFixo");
    driver.manage().window().maximize();

    // Logar BkoMais
    driver.findElement(By.id("matricula_I")).sendKeys("844502");
    driver.findElement(By.id("senha_I")).sendKeys("Pw34Jdt#*");
    driver.findElement(By.id("bt_entrar")).click();

    // Logar na Estratégia
    driver.findElement(By.id("mn_backoffice")).click();
    driver.findElement(By.id("mn_bkoffice_prod_203")).click();// Produto
    driver.findElement(By.id("mn_bkoffice_est_57")).click();// Estratégia

    // Selecionado a atividade
    Select atividade = new Select(driver.findElement(By.id("cboAtividade")));
    atividade.selectByIndex(3);

    // Registro >> Novo
    Thread.sleep(500);
    driver.findElement(By.id("mn_registro")).click();
    driver.findElement(By.id("mn_novo_caso")).click();

    // Cod Os Estratégia VREL
    String CodOs = driver.findElement(By.xpath("//*[@id=\"content\"]/div[1]/fieldset[1]/div[2]/div[3]/span"))
            .getText();
    System.out.println(CodOs);

    // Campo Análise de Contrato
    Select analiseContrato = new Select(driver.findElement(By.id("cboMotivo")));
    analiseContrato.selectByIndex(5);

    // Campo Ação
    boolean breakIt = true;

    while (true) {

        breakIt = true;

        try {

            Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
            acao.selectByValue("519");

        } catch (Exception e) {

            if (e.getMessage().contains("element is not attached")) {

                breakIt = false;
            }
        }
    }

    Select status = new Select(driver.findElement(By.id("cboStatus")));
}

@After
public void tearDown() throws Exception {

}}

Unreachable Code

Kakashi - Sensei
  • 371
  • 1
  • 6
  • 22
  • Possible duplicate of [Unreachable code in eclipse](https://stackoverflow.com/questions/19098132/unreachable-code-in-eclipse) – Mihai Chelaru Jun 07 '18 at 18:04
  • 2
    If you are using this code because of [this answer](https://stackoverflow.com/a/31061804/1707091), then be aware of the comment on the answer that says, "This makes no sense what so ever." and is upvoted 15 times. At least use `breakIt` in the `while` condition. – rgettman Jun 07 '18 at 18:09
  • rgettman, the code with the while I got on the internet to solve the error of ** element is not attached to the page document **. After healing the error, I continued with the automation but Eclipse displays the error message: Unreachable code. – Kakashi - Sensei Jun 07 '18 at 18:17

1 Answers1

0

As I am understand correct, there's no guarantee for compiler that code above while statement will run. It will run only when there would be exception thrown and the while-loop breaks. Moreover your breakIt isn't changing at all. As I think right this code will be work right:

while (breakIt) {

    breakIt = true;

    try {

        Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));
        acao.selectByValue("519");

    } catch (Exception e) {

        if (e.getMessage().contains("element is not attached")) {

            breakIt = false;
        }
    }
}