Selenium WebElement value empty after sending keys - selenium-webdriver

I'm running some simple form tests where values are added fields.
After each value is added to a field:
input.SendKeys(value);
I want to check the value in the field is correct. This may sound unusual but the field may have an ajax search attached and if the search doesn't pull back a match, the field will be empty.
I've tried testing the text value of the WebElement after sending the keys but it always seems to be empty:
bool match = input.Text.Equals(value);
// input.Text always seems to be an empty string
I'm using Selenium 2 with the WebDriver - is there another way to perform these checks? Is there a particular reason why the WebElement is empty even if the SendKeys successfully prints a value (actually in the browser) in to the WebElement (textbox)?
Any help would be grateful.

It may be possible that the text value that you are entering is assigned as a "value" attribute of text box and not as "text"
input.sendKeys(enteredValue)
String retrievedText = input.getAttribute("value");
if(retrievedText.equals(enteredValue)){
//do stuff
}

Related

How to send integer value in a text box which accepts integer only using selenium?

I am using selenium webdriver and I am using Sendkeys method to set "1" as integer in the text box. But when i click on proceed it reset the text box to 0 as if no valid value.
Manually when i do the same thing ,it is happening.
Later I came to know that Sendkeys("1") method is sending 1 as string not as integer.
How to deal with this thing? One way i found is java script executor.
Is there any other better way?
Looking for positive response
If it is working in manual approach, try this:
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_1);
rob.keyRelease(KeyEvent.VK_1);
I'm working in Java, but in C# should be very similar, maybe even same.
Use the following code:
public void sendKeysINTByJS(WebDriver driver, WebElement element, int attributeValue){
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("arguments[0].setAttribute('value','"+attributeValue+"');", netQtty);
}
You can also use this for strings by changing the parameter type.

isEnable() is not working in selenium webdriver for find the button is enabled or disabled?

i am trying the bellow code,but always return true value.
if(updatebuton_status=="true"){
Thread.sleep(timmer*3);
//click on the update button
//ieDriver.findElement(By.xpath("html/body/div[1]/div/div/div/div[3]/div/div[1]/div/div/table/tbody/tr/td/table/tbody/tr/td[3]/div/div/div[2]/div[3]/div/div/div/div[2]/div/div[2]/div[2]/div/div/div[3]/div[2]/div/div/div/div[3]/div/div/div/div/div/div[2]/div[1]/div/div/div/div/table[1]/tbody/tr/td/table/tbody/tr/td[3]/span/table/tbody/tr[2]/td[2]")).click();
ieDriver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[3]/div/div[1]/div/div/table/tbody/tr/td/table/tbody/tr/td[3]/div/div/div[2]/div[3]/div/div/div/div[2]/div/div[2]/div[2]/div/div/div[3]/div[2]/div/div/div/div[3]/div/div/div/div/div/div[2]/div[1]/div/div/div/div/table[1]/tbody/tr/td/table/tbody/tr/td[1]/span/table/tbody/tr[2]/td[2]")).click();
}else{
Thread.sleep(timmer*3);
//click on the Add button
ieDriver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[3]/div/div[1]/div/div/table/tbody/tr/td/table/tbody/tr/td[3]/div/div/div[2]/div[3]/div/div/div/div[2]/div/div[2]/div[2]/div/div/div[3]/div[2]/div/div/div/div[3]/div/div/div/div/div/div[2]/div[1]/div/div/div/div/table[1]/tbody/tr/td/table/tbody/tr/td[1]/span/table/tbody/tr[2]/td[2]")).click();
}
I think somewhere you are doing wrong as your xpath is too long recommended to use relative xpath. and if your element having id then use it directly
I don't know how you are checking that element is displayed
use following method to do same -
boolean check= driver.findElement(By.id("tVLQur-btn")).isEnabled();
if(check) // if `check` returns true then go in if condition
{
driver.findElement(By.id("tVLQur-btn")).click();
}
else
{
// DO SOME OTHER FUNCTIONALITY
}
Edited
If your button id is dynamic then use following xpath to find the element
//td[contains(.,'Update')]/../preceding-sibling::tr//button
like
boolean check= driver.findElement(By.xpath("//td[contains(.,'Update')]/../preceding-sibling::tr//button")).isEnabled();
It will find your update button and check weather it is enable or not
I believe at the very least if you break the line into two statements, one to located the element, and the next to check its enabled status, at least you'll know that the problem (most likely) lies in the element location, as it would return a nul to the web element and isEnabled would return a null pointer exception. There are always ways to use relatively short xpaths if no consistent ID is available. Check your browser for add-ins that include the word xpath, and you'll see there are a variety of xpath helpers out there. The inspect element option is the safest, at least in theory, but it's not always the best.

How to Print the Disabled text from Geb/Groovy?

enter image description here
We are automating the UI Application, Our UI application have Disabled Text are present, so we need to Validate the Disabled text. Before validating, I have to Print the Disabled text, Please guide me to how to print the text using Geb/Groovy.
Please find the Image of HTML tag which i highlighted is the Disabled text
BNSF0000712570
BNSF0000712570
The selector above will yield multiple results, i.e. elements, if there is more than one element that matches the classes used in the By.cssSelector query.
To get only the element containing "BNSF0000712570", I would suggest you try to get it using the "ext:qtip" attribute instead (which I assume is unique per element containing a disabled text) on the div containing the disabled text:
def myText = $(“div[ext:qtip=‘Id: 0001’]”).text();
println myText;
assert myText == "BNSF0000712570";
#Saurabh Gar: Why would you use the WebDriver "By" class selectors? With Geb you have access to a wide range of simpler ways to write selectors, e.g. like the one used above.
You should try using By.cssSelector as below :-
def text = driver.findElement(By.cssSelector("td.x-grid3-td-elementvalue").text
Or
def text = driver.findElement(By.cssSelector("div.x-grid3-col-elementvalue").text
assert text == "BNSF0000712570"
println text
Note:- If still doesn't get the text need to share table HTML insteadof screenshot that's why, could make a best locator.
Hope it helps..:)

Codename One - get selected text from AutoComplete

How can I get the complete selected text from an AutoComplete TextField?
If I use getText(), I only get the few letters the user has input so far.
Example: I write "flo" and then select "Flowers" from the list, but getText() gives me "flo"
AutoCompleteTextField auto = new AutoCompleteTextField(arrayWithNames);
auto.setMinimumLength(4);
auto.addListListener((ActionEvent evt1) -> {
String lookedFor = auto.getText();
Hashtable<String,Object> match[] = findMatch(lookedFor);
if(hMatch.length>0){
contElements.removeAll();
for (Hashtable<String, Object> Match1 : match) {
...
...//fill the Container with the names found
...
}
}
});
How it works
I am using the AutoComplete TF as a search button.
I have an array with all the names in my list.
Then I populate the Auto with the array.
The user selects a name from the Auto and then I search the value that is being "lookedFor" using the findMatch(). It returns a new array with the found entries.
I need the complete name from the list so I can use the findMatch() method, but when I use getText() from the Auto, it only returns the letters the user entered, and not the whole name, so my method does not work, since I am comparing whole Strings.
(I am using the Auto because it is very convenient if people remember only a part of the name they are looking for)
If you subclass AutoCompleteTextField you can access the selected text internally via getSuggestionModel().getItemAt(getSuggestionModel().getSelectedIndex()). Now you can define a public getter method getSelectedText() or something on your derived class.
I am not sure you are using the AutoCompleteTextBox correctly.
The entire purpose of the AutoCompleteText box is to help you assist the user in selecting from a list of valid requests,
You should not be getting the value of getText() until the user is ready to submit the form where the AutoCompleteTB is located.
This WILL help if you haven't already looked here:
https://www.codenameone.com/javadoc/com/codename1/ui/AutoCompleteTextField.html#getPropertyTypes--
Good luck!

Webdriver visibility of element without locator

I want to check the text of my string(client) inside tag using the following code:
boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
If it returns true then, Is there any way to directly check the visibility of this string (without locator)?
Find an element that contains the needed text in its text node:
WebElement element = driver.findElement(By.xpath("//*[text()='" + client + "']"));
If needed, use normalize-space() or contains() instead:
WebElement element = driver.findElement(By.xpath("//*[contains(text(),'" + client + "')]"));
This will find the innermost element containing the text, not its any random ancestor.
Check whether it's visible via
element.isDisplayed()
Note that you have to be sure your text only appears once on the page for this to be okay. But the solution can be easily adapted for more elements, too.

Resources