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();
}
}
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.