我试图通过ID在页面上找到一个元素,但即使该元素有ID,selenium也会显示无法找到该元素的消息。
我创建了一个名为InvalidLogin的类,它扩展了BaseClass类。在InvalidLogin类中,有一个名为fillTreatmentOps的方法。在这个方法中,我尝试通过ID(cboMotivo)找到元素,但我不能。下面是课程代码和屏幕打印。
public class BaseClass {
WebDriver driver;
@BeforeClass
public void setupApplication() {
Reporter.log("Instanciando o navegador", true);
System.setProperty("webdriver.chrome.driver", "e:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://10.5.9.45/BkoMais_Selenium/");
Reporter.log("Aplicação inicializada", true);
}
@AfterClass
public void closeApplication() {
try {
Thread.sleep(5000);
//driver.quit();
Reporter.log("Sessão do navegador finalizada",true);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class InvalidLogin extends BaseClass {
@Test(description = "Logando na aplicação")
public void logarBkoMais() {
try {
driver.findElement(By.id("matricula_I")).sendKeys("844502");
driver.findElement(By.id("senha_I")).sendKeys("Bcc201707");
driver.findElement(By.id("bt_entrar")).click();
// driver.navigate().back();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test(description = "Acessando a estratégia BackOffice >> NET >> Facilidades")
public void logarEstrategiaNetFacilidades() {
driver.findElement(By.id("mn_backoffice")).click();
driver.findElement(By.id("mn_bkoffice_prod_217")).click();
driver.findElement(By.id("mn_bkoffice_est_179")).click();
}
@Test(description = "Registro >> Novo")
public void registroNovo() {
driver.findElement(By.id("mn_registro")).click();
driver.findElement(By.id("mn_novo_caso")).click();
}
@Test(description = "Facilidades >> Tratamento Os")
public void preencherCamposTratamentoOs() {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement analiseC = wait.until(ExpectedConditions.elementToBeClickable(By.id("cboMotivo")));
driver.findElement(By.id("cboMotivo")).click();
}
}
答案 0 :(得分:0)
这个问题使我震惊,因为我发现添加time.sleep(2)允许Selenium firefox驱动程序找到该元素,而我却无法找到它到底是怎么回事。
time.sleep(2)
try:
action_button = WebDriverWait(driver, TIMEOUT).until(
EC.visibility_of_element_located((By.ID, "id_btn"))
)
action_button.click()
没有明确的睡眠就无法工作(2)