How should I approach the html placeholder attribute scenario automation with webdriver - selenium

Hi I need to automate a sceanrio where a text input field is there, where a text -
"type here" is displayed.
<input class="textbox" type="text" value="" placeholder="type here" name="q">
I need to verify
1) Text is displayed - "type here"
2) When focus is moved to this text field, the text input field does not display -
"type here"

You can test whether the browser supports the placeholder attribute for <input> elements via a simple JavaScript. This is an indirect test, but as far as I know, you can't test for the placeholder text via Selenium. But it should be absolutely sufficient:
function supportsInputPlaceholder() {
var input = document.createElement('input');
return 'placeholder' in input;
}
(taken from http://diveinto.html5doctor.com/detect.html#input-placeholder)
Therefore:
// only if your driver supports JS
JavascriptExecutor js = (JavascriptExecutor)driver;
String placeholderTest = "return 'placeholder' in document.createElement('input')";
assertTrue(js.executeScript(placeholderTest));

Related

Cannot Input Text into HTML input element using Robot Framework

I have HTML which looks like this:
<input type="text" name="USERNAME.DUMMY.DUMMY.1" value="" id="USERNAME.DUMMY.DUMMY" class="username" size="25" autocomplete="Off">
I am trying to insert text to this input element after loading it. Her is my Robot script:
Open Browser ${url} ${browser}
Sleep ${wait}
Input Text id=USERNAME.DUMMY.DUMMY ${text}
But it is never able to find the ID. I have tried the same with class and name attributes but none of them work. This element is not under an iFrame.
Any help would be appreciated !
Can you try
input text //*[#id="USERNAME.DUMMY.DUMMY"]

How to write selenium code for autocomplete text box withe list selection

I'm trying to write Selenium code for below HTML source code..
This field is the auto populated field for input selection
<input id="ctl00_ContentPlaceHolder1_txtBranch" class="textbox_service ui-autocomplete-input" name="ctl00$ContentPlaceHolder1$txtBranch" style="width: 200px;" onblur="return branch();" onchange="return CheckBranchName();" tabindex="6" autocomplete="off" type="text"/>
Any one can help me out to write the code?
Web element screen shot attached.
Thanks in advance.
This is the best I could do with the information you provided. If you could show the HTML for what the autocomplete list looks like that would be great. You didn't specify any language so I'll assume it's Java.
WebElement field = driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtBranch"));
field.click();
field.sendKeys(Keys.SPACE);
List<WebElement> items = driver.findElements(By.tagName("li");
for (int i=0; i<items.size();i++) {
WebElement elementYouWantToClick = items.get(i);
String x = elementYouWantToClick.getText();
if(x.contains("TextThatIsInYourElementYouWantToChoose")){
elementYouWantToClick.click();
}
Best I could do for now with such limited information.
As per the HTML to click(select) the Auto Complete text, you can use the following line of code :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='textbox_service ui-autocomplete-input' and contains(#id,'_ContentPlaceHolder') and contains(#name,'txtBranch')]"))).click();

How to get values from disabled inputs in webdriver

My html code is -
<input id="txtPortalLogin" class="form-control input-sm" type="text" disabled="disabled" placeholder="No Link" value=""/>
Please assist some code to get the values from disabled field.
Screenshot is attached so that you will find the which text values i am talking about.
Input Fields are disabled and I want values from shown screenshot
I know how to get this value with Python code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('your_page_url')
disabled_input_field = driver.find_element_by_id("txtPortalLogin")
value = disabled_input_field.get_attribute('value')
Use the javascript executor in selenium to execute a javascript code which return the value of the html element ( input). Something simmilar to the below
String value = "";
if (driver instanceof JavascriptExecutor) {
String value = (String)((JavascriptExecutor) driver)
.executeScript("return document.getElementById('txtPortalLogin').value");
}
In C# it would be
driver.FindElement(By.Id("txtPortalLogin")).GetAttribute("value");
however you could also do this using the JavaScript executor as so:
var value = ((IJavascriptExecutor)driver).ExecuteScript("return $('#txtPortalLogin').attr('value')").ToString();
EDIT; I think the OP might want to get the https://production etc part out of the field. I'm not 100% sure how to do that, sorry.
I did also find this link which might be able to answer your problem; TLDR is make it readonly instead of disabled which allows the value behind to still be accessible but not changeable by the user.
Usually driver.getAtribute("value"); works. But as value field is empty, you'll have to go for JavaScriptExecutor -
JavascriptExecutor je = (JavascriptExecutor) webDriver;
String value = je.executeScript("return angular.element(arguments[0]).scope().{{modalValue:put modal value from HTML}};", {{webElement}}).toString();
return value;

WebDriver SendKeys Doesn't Send Value

I am trying to run selenium webdriver, imitating typing something to textbox and submit it.
I have this following code:
WebElement element = null;
element = waitForElementPresent(tFByCssSelector,timeoutValue);
element.clear();
element.click();
element.sendKeys("Input String");
The code successfully type "Input String" to the textField, but when I submit the form, it says the form is empty (The form had been set to catch empty input exception).
I wonder why sendKeys does not set the value of the text field even though it has typed the wanted value into the text field.
Try to tab out of the field:
element.sendKeys("Input String");
element.sendKeys(Keys.TAB);
It may be that the field value only gets set on blur.
As I see, all looks okay in your selenium code. I believe there is some problem with your HTML form.
The form should have proper action for receiving the values. A sample form is provided below.
<form action="http://foo.com" method="post">
<input name="say" value="Hi">
<input name="to" value="Mom">
<button>Send my greetings</button>
</form>
Based on what you use in method parameter, you will get the form data in POST or GET variables.
Also you don't need to click on the element before sending keys in Selenium.

Getting the default value (text) of an <input> element with selenium webdriver

I wonder about how I can use selenium webdriver to find the default text of an element ?
In the browser, the input field displays a default value: 'Project 1', but I cannot get this text through the method getText() of this WebElement.
<input class="title viewData" id="sprojectName" maxlength="255" name="projectName" type="text" projectinfo="1">
getText() returns "the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace." You need something like getAttribute("value") or getAttribute("placeholder").
The getText() method is for retrieving a text node between element tags for example:
Eg:
<p>New</p>
But usually the value in the text box is saved to "value" attribute. So the below statement will work:
findElement(By.id("ElementID")).getAttribute("value");
Yes, I will try to see if getAttribute("value") work. In the meantime, I have solved the problem using JavaScript executor:
String jsStatement = "return document.getElementById('" + elementId + "')." + "value" + ";";
JavascriptExecutor js = null;
if (session instanceof JavascriptExecutor) {
js = (JavascriptExecutor)session;
}
return (String) js.executeScript(jsStatement);