我想使用By.id或By.cssSelector getText().
我设法通过执行getAttribute(“value”)来解决我的问题,但我不明白为什么getText()不能像我期望的那样工作,我可能需要它所以所有的帮助都很感激.
这是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"
和HTML:
<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/>
解决方法
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getText()
getText()提供WebElement的innerText.
您的输入字段没有任何内部文本.文本位于value-attribute中,因此通过getAttribute(“value”)访问它是正确的方法.