Element not found in the cache - perhaps the page has changed since it was looked up - Command duration or timeout - selenium

All I am trying to achieve is an assertion, that text entered in the description field displays after the update button is hit, but I am getting an error that I mentioned in the Title above. Any help is greatly appreciated!
FYI - page does refresh after user hits the update button, however, I don't notice any change in the url though!
My code:
//send text to the description text field
WebElement description = driver.findElement(By.id(PvtConstants.UPDATE_KEYNAME_DESCRIPTION_FIELD));
description.sendKeys(keyname_description);
//verify the text (this is the assertion)
verifyDisplay(description.getAttribute("textContent") + "", By.id(PvtConstants.UPDATE_KEYNAME_DESCRIPTION_FIELD));
HTML:
<li>
<span>2.</span>
<label class="input_label" for="keyname_description">Description</label>
<textarea style="line-height:normal;" id="keyname_description" name="description" maxlength="256">this is my description text</textarea>
</li>

In case of text field or text area, you need to get values using the attribute value. **description.getAttribute("value")**. I guess this is throwing the error as it could not find the attribute textContent.

Use Javascript Executor to get the text from text area.Let me know if this code doesn't work for you.
JavascriptExecutor js=(JavascriptExecutor) driver;
String description_text=(String) js.executeScript("return document.getElementById('keyname_description').innerHTML");
System.out.println(description_text);

Related

Not able to Enter Text in a TextBox using Selenium Webdriver

I have a TextBox that appears on Radio Button Selection and I have to enter values in a particular format(XYZ989898-99). However I am unable to do so by using the commonly used methods of entering text in selenium.
Background info related to Textbox :
TextBox appears when user clicks on Yes radio button.
TextBox has default value as XYZ already entered.
SendKeys do not concatenate the text given without XYZ as well.
TextBox is inside an Iframe to which focus is shifted correctly.
The code does not fail, it just remained clicked on textbox without entering anything.
Approach 1 :
driver.findElement(locator).SendKeys("989898-99");
Approach 2 :
driver.findElement(locator).clear();
driver.findElement(locator).SendKeys("XYZ989898-99");
Approach 3 :
WebElement element = driver.findElement(locator);
Actions action1 = new Actions(driver);
action1.movetoElement(element).click().perform();
action1.sendKeys("XYZ989898-99");
HTML Sample :
<div class="value" ng-show="selectedValue" style="">
<label class="labelAboveTextBox">XYZ Number*</label>
<input type="text" class="Value" ng-model="XYZNumber" ng-
req="XYZNumber" xyz-input="" maxlength="12" style="value"
id="value" value="XYZ" required="required">
<!-- ngIf: invalidtspmessage -->
<span style="font-family: 'NeueHaasGroteskText';font-size:
0.7rem;color: black;">
Note: Enter XYZ is this format: XXXXXXXXX-XX
</span>
</div>
I would like to know If there is any other approach that can be used to get this problem resolved and also why does the above approaches failed to work.
You have used the wrong methods for sending the keys. (in approach 1 and 2)
Instead of .SendKeys(), you have to use .sendKeys() method for writing anything on UI side
Into the third approach, you have used the wrong method .movetoElement(). The method name is .moveToElement().
For sendKeys() don't need to use Actions class. basically, it is for Keyboard events and for mouse events.
(WebElement).sendKeys("989898-99"); // first approach you can use
(WebElement).clear(); // second approach you can use
(WebElement).sendKeys("XYZ989898-99");
Hope this will help you to solve your errors.
Other way you can use is to set the text is by using the JavaScriptExecutor. Give some delay before performing the action.
Try the below sample code :
Thread.sleep(3000);
((JavascriptExecutor) driver).executeScript("document.getElementById('value').value='XYZ989898-99';");
As the id is unique, you can use the JavaScript's getElementById() function to set the value.
If you want to set the text through the sendKeys() method and before that if you want to clear the text then simple clear() may not work instead you can try to delete the XYZ values first and try to send the values like below again :
// Wait for some time
Thread.sleep(3000);
// Locate the element first and store it in some variable
WebElement element = driver.findElement(By.id("value"));
// Fetch the existing text from the field first
String existingValue = element.getAttribute("value");
// Wait for some time
Thread.sleep(3000);
// Click on that element first so the focus will shift to there
element.click();
// Wait for some time
Thread.sleep(2000);
// Loop until the existing value length
for(int i=0;i<existingValue.length();i++) {
// Remove the existing character text one by one
element.sendKeys(Keys.BACK_SPACE);
}
// Try to send the text at the end, make sure that you should append XYZ as a prefix
element.sendKeys("XYZ989898-99");
I hope it helps...

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();

First character is missing inconstantly while sending string to the ExtJS input via sendKeys()

I randomly face the issue of missing first character in the ExtJS5 input field, while sending string via sendKeys method.
System info:
Ubuntu 14.04 -> docker containers with selenium grid (2.48.2)
Browser Firefox
Code is simple. I just get input web element, wait if it's clickable (i.e. isEnabled and isDisplayed), clear and send string:
wait.until(ExpectedConditions.elementToBeClickable(input)).clear();
input.sendKeys(value);
input element is simple too:
<input id="textfield-1455-inputEl" data-ref="inputEl" type="text" role="textbox" size="1" name="name" class="x-form-field x-form-required-field x-form-text x-form-text-default x-form-focus x-field-form-focus x-field-default-form-focus" autocomplete="off" componentid="textfield-1455"/>
I've noticed that issue occurs only for the first sendKeys() executing on the page:
Enter the page, wait for page load, work with first input
Enter the page, wait for page load, choose Enable into corresponding select box in order to enable input field, work with input field (image with this example is attached)
Enter the page, wait for page load, click button add in order to add the needed input field, work with input field
Other occurrences of the sendKeys on the page are stable.
I've looked for similar questions. It does not seem the issue with special characters (Missing characters example: 46-> 6; coverTest -> overTest; 1 -> nothing);
Also, I don't think it's an issue with missing characters due to remote webdriver infrastructure. The tests fail randomly but in exact places.
I know that I can use sendKeys(), then check the value of the input and repeat the sending action. However, it's the last option.
Is there any additional check needed for ExtJS input (any attribute in DOM) in order to be sure that input field is ready?
Appreciate your help.
Some times it happens with me. Try clicking on to the field first, but it's a wild guess assuming there can be some focus related issues.
Your sequence could be somewhat like this:
wait.until(ExpectedConditions.elementToBeClickable(input)).click();
input.clear();
input.sendKeys(value);
Weird thing is that I actually faced a situation, where I clicked it twice before sending values and it worked somehow :P
Another thing to try could be using a non-native javascript executor.
JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
myExecutor.executeScript("arguments[0].value='6';", input);
Sorry man, if the system would have been in front of me I'd have tried much more things.
I was struggling with sendKeys failing my self, but the following works pretty consistently. The method findVisibleElement is a custom wrapper for driver.until....
protected static boolean sendKeysByChar(By by, String input)
{
WebElement field = driver.findVisibleElement(by).base();
field.click();
field.clear();
for (int i = 0; i < input.length(); i++) {
String current = driver.findElement(by).getAttribute("value");
String nextChar = String.valueOf(input.charAt(i));
while (current.length() <= i || !current.endsWith(nextChar)) {
field.sendKeys(nextChar);
current = driver.findElement(by).getAttribute("value");
}
}
field = driver.findElement(by); // Refresh element
if (field.getAttribute("value").equals(input)) { return true; }
log.warn("Send keys by char failed.");
return false;
}

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.