0

I try make a automation test using Page Object with annotation By in selenium webdriver, but de Eclipse show-me the following message error:

java.lang.ClassCastException: org.openqa.selenium.By$ById cannot be cast to org.openqa.selenium.WebElement

Follow the code:

Class: AcertoPerfilTratamentoOs

public class AcertoPerfilTratamentoOs {

    static WebDriver driver;

    By cidade = By.id("cboCidade");
    By tipoOcorrencia = By.id("txtTipoOcorrencia");

    public AcertoPerfilTratamentoOs(WebDriver driver) {
        this.driver = driver;
    }
    public void camposCidade(String CampoCidade) {
        Select slc = new Select((WebElement) cidade);
        slc.selectByVisibleText(CampoCidade);
    }
    public void campoTipoOcorrencia(String tipOcorrencia) {
        driver.findElement(tipoOcorrencia).sendKeys("reclamação");
    }
}

Class: ValidarEstrategiaAcertoPerfilLancamentoManualTest

public class ValidarEstrategiaAcertoPerfilLancamentoManualTest {

    static WebDriver driver;

    @Before
    public void setUp() throws Exception {  
        SelecionarNavegador nav = new SelecionarNavegador();
        driver = nav.iniciarNavegador("chrome", "http://10.5.9.45/BkoMais_Selenium/");
    }

    @Test
    public void logarAplicacao() {      
        try {
            //Login Page
            LogarBkoMaisPage login = new LogarBkoMaisPage(driver);
            login.logar("844502","Bcc201707");

            //Acessar a estratégia
            ProdutoNetEstrategiaAcertoDePerfilLancamentoManual AcertoPerfil = 
                    new ProdutoNetEstrategiaAcertoDePerfilLancamentoManual(driver);
            AcertoPerfil.AcessarEstrategia();

            //Registro Novo
            RegistroNovoCasoPage novoCaso = new RegistroNovoCasoPage(driver);
            novoCaso.registrarCaso();

            //Preenchendo o campo OCORRÊNCIA
            RandowNumber rn = new RandowNumber(driver);
            rn.randomNumber();

            //Preencher Campos da tela Tratamento Os
            AcertoPerfilTratamentoOs po = new AcertoPerfilTratamentoOs(driver);
            po.camposCidade(" ALMIRANTE TAMANDARE ");
            po.campoTipoOcorrencia("reclamação");

        }catch(Exception e) {
            System.out.println("Mensagem de erro: " +e);
        }
    }

    @After
    public void tearDown() throws Exception {
        //Thread.sleep(5000);
        //driver.quit();
    }
}

ValidarEstrategiaAcertoPerfilLancamentoManualTest

1 Answer 1

2

The error message is telling you what is wrong: org.openqa.selenium.By$ById cannot be cast to org.openqa.selenium.WebElement I'm assuming this is caused by: Select slc = new Select((WebElement) cidade); Try to following instead (assuming Select has a constructor that takes a WebElement: Select slc = new Select(driver.findElement(cidade));

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! For future learning: pay attention to what the error message is telling you. If you don't know what it means, Google it. You'll be able to fix a lot of things yourself that way. And otherwise you can still ask StackOverflow ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.