Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SeleniumWebDriver中的不可访问代码_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java SeleniumWebDriver中的不可访问代码

Java SeleniumWebDriver中的不可访问代码,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,在使用SeleniumWebDriver进行自动化的Java脚本中,没有将元素附加到页面文档中 我搜索了互联网,发现下面的脚本解决了页面文档未附加元素的错误。 boolean breakIt = true; while (true) { breakIt = true; try { Select acao = new Select(driver.findElement(By.id("cboSubMotivo")));

在使用SeleniumWebDriver进行自动化的Java脚本中,没有将元素附加到页面文档中

我搜索了互联网,发现下面的脚本解决了页面文档未附加元素的错误。

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")));
此时Eclipse显示错误消息:无法访问代码

我搜索了互联网,但没有找到任何关于SeleniumWebDriver的错误消息

下面是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 {

}}

我的理解是正确的,编译器不能保证上面的代码while语句会运行。只有当抛出异常并且while循环中断时,它才会运行。此外,您的
breakIt
根本没有改变。正如我认为正确的那样,此代码将正常工作:

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;
        }
    }
}

如果您使用此代码的原因可能是重复的,那么请注意答案上的注释:“这毫无意义”,并且被提升了15次。至少在
while
condition.rgetman中使用
breakIt
,在我上网解决**元素错误时使用的代码没有附加到页面文档**。修复错误后,我继续进行自动化,但Eclipse显示错误消息:无法访问代码。