2014-05-22 17 views
6

Voglio getText() utilizzando By.id o By.cssSelector.Selenio getText

Sono riuscito a risolvere il mio problema facendo getAttribute ("valore"), ma non capisco perché getText() non funzioni come mi aspetto, e potrei averne bisogno in modo che tutto l'aiuto sia apprezzato.

Ecco il java:

WebDriverWait wait = new WebDriverWait(driver, 10);   
Boolean elementIsPresent = wait.until(ExpectedConditions.textToBePresentInElementValue(By.cssSelector("#general_service_name"),"[reg] general_service_name")); // true   

//WebElement general_service_name = driver.findElement(By.cssSelector("#general_service_name")); 
WebElement general_service_name = driver.findElement(By.id("general_service_name")); 

// Display check 
Boolean isDisplayed; 
if(general_service_name.isDisplayed()) isDisplayed = new Boolean(true); else isDisplayed = false; //true 

String text_empty = general_service_name.getText(); //"" 
String text_with_value = driver.findElement(By.id("general_service_name")).getAttribute("value"); //"[reg] general_service_name" 

e HTML:

<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/> 
+0

getText restituisce solo testo visibile e NON restituisce alcun elemento HTML e NON restituisce alcun testo nascosto. Ad esempio, con Java stavo riscontrando questo problema con un tag BR che restituisce vuoto. Invece di '.getText()' Ho usato '.getAttribute (" innerHTML ")' che restituirà ciò che stavo cercando, incluso qualsiasi HTML che è invisibile o testo che è nascosto. – jsherk

risposta

0

Risposta semplice: è progettato in questo modo. getText() analizza il contenuto del tag (cioè il suo innerText), che è ovviamente vuoto per gli input.

2

Java ele.getAttribute("innerHTML");

Questo potrebbe ottenere il testo già in background e non visualizzato sulla pagina ancora.

+0

Questo è stato davvero utile in una situazione in cui getText() ha restituito una stringa vuota ma la soluzione ha restituito il testo interno dell'elemento . Grazie ! – Chris