Need to pass data to hidden text field in Selenium - selenium

I got the below solution here,
jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");
and then passing data using SendKeys.
But it is creating duplicate text field with text attribute and hidden text field still exist..

You have two input tags. I am assuming you want to execute the script against the second one not the first.
Also, I am using querySelector and that allows you to pass cssSelector to identify the element you want.
Note: Make sure the format of dateToPass is correct
String dateToPass = "01/01/2015";
String scriptText = "document.querySelector('.propertyYear.require').setAttribute('value','" + dateToPass + "')";
((JavascriptExecutor)driver).executeScript(scriptText);

Related

How to locate random id generated by a modal?

I was testing my website using RF. The problem is, every time the modal is opened, a different id(locator) will be set on the textbox that I want to input my text. How do you get value of this locator?
I was supposed to try Get Element Attribute but then it cannot support my problem since it still requires a specific locator.
In ROBOT Framework (RF), the locator can be accessed by several ways. Please refer and read this link: http://robotframework.org/Selenium2Library/Selenium2Library.html
The most common way to access the locator is by id such as :
Input Text id:username # Element with id 'username'.
Input Text id:password # Element with id 'password'. you can also use 'Input Password' keyword.
However, if the 'id' element is so dynamic which it keep changing, then the best alternative is to use either ABSOLUTE XPATH expression or CSS selectors. Install the XPATH add-on in your web browser. For firefox, just install ChroPath.
Then, get the ABSOLUTE Xpath element of that username & password text box. Let's assume we know the absolute xpath expression already, so in ROBOT, you can write like below.
${login_absolute_xpath}= Set Variable xpath=/html[1]//div[7]/form[1]/div[1]/input[1]
${password_absolute_xpath}= Set Variable xpath=/html[1]//div[7]/form[1]/div[2]/input[1]
Wait Until Page Contains Element xpath=${login_absolute_xpath}
Input Text xpath=${login_absolute_xpath}
Input Text xpath=${password_absolute_xpath}
...
This should works. Please let me know if this helps.

clicking on dynamic values in a web table in selenium

Below is my Table structure:
I want to click on the first cell of "policyno " column only if value is not empty
How do I achieve this?
Since you didn't specified, I have no idea what programming language do you need, but the idea is the same for all. I've used C# for the code below:
Get all the policy number elements. This can be easily achieved by getting the elements by xpath. In your case, I expect the lblPolicyNumber to be present in all:
IList allPolicyElems = driver.FindElements(By.Xpath(".//*[contains(#id,'lblPolicyElements')]"));
Now, you have 2 options to click on the element you need. First, you click on an element using his position in the list: allPolicyElems[0].Click(); (not the best way to do it) or by using LINQ (or lambda expressions for Java) to get the element by the text (perhaps you have the text stored in a variable from some previous actions): allPolicyElems.FirstOrDefault(t => t.Text == "your_text_here").Click();
This can be further expanded by applying the same logic in case you need to get the correct element by knowing other cell values from the same row.
Try using this.
WebElement elem = driver.findElement(By.xpath("//table/tbody/tr[2]/td[3]/span/a"));
if(!(elem.getText().trim().equals(""))){
elem.click();
}

extracting part of success text using selenium webdriver

I have the following text appearing on the success page of my application.
This is to confirm that your application has been received. Your Order Number is “#00007942”. If further instructions or any clarification is needed regarding your application, a representative will contact you.
Complete text having same property.
Please help me in extracting the value 00007942 and store it in variable.
First, get your text in your way.
String successMessage = driver.findElement(By.cssSelector("your selector")).getText(); // use locator of your wish
Now, use replace all non-digit from your string as follows-
String orderNumber = successMessage.replaceAll("\\D+", ""); // this replaces all non-digits from your previous string
there is no way to retrieve partial text in selenium webdriver.
Instead, you access the complete text of an Web Element using element.getText() in Java or element.text in python and store it as a String variable.
Then you process the string to retrieve the substring you want.
In all programming languages, there are many ways to achieve it. some of them are substring method, regular expression.

WebDriver not identifying WebElement

I using xpath="//div[#class='localityKewordDropDown']/descendant::div[#class='over']/span[text()='Dwarka, New']
but the element is not getting recognized. NosuchElementException is getting encountered.
Could anyone help me out here.I want to click the drop down value highlighted in the image.
It's failing beacuse there is a space after the word 'New'. The following should work.
//div[#class='localityKewordDropDown']/div/div[text()='Dwarka, New ']
Or consider serarching for the element containing the text rather than matching the entire text.
//div[#class='localityKewordDropDown']/div/div[contains(.,'Dwarka, New')]
And as cathal mentioned, you are searching for a div and not a span.
EDIT:As you request (although I don't believe there is a difference between "descendent" and "//".
//div[#class='localityKewordDropDown']/descendant::div[text()='Dwarka, New ']
//div[#class='localityKewordDropDown']/descendant::div[contains(text(),'Dwarka, New')]
Contains is an xpath function that allows you to query on something containing a value. It can be used with attributes, nodes but it's generally most useful for finding elements containg text. The reason this is working where as your query for the exact string fails is because the element you seek is padded with a trailing space. The contains query will find the element you are seeking as it's ignoring this trailing space.
i dont see anyw spans around your element, try the following:
WebElement name = driver.findElement(By.xpath("//div[#id='keyword_suggest']//div[text()='Dwarka, New']"));
Try this:
WebElement name = driver.findElement(By.xpath("//*[contains(text()='Dwarka, New')]"));

FindElement does not return element text unless actually in DOM

I'm using Selenium's webdriver, but I've hit upon a problem.
I'm using KnockoutJS to bind my UI to data from the server. In my tests when I call FindElement(By.Id("InputField")) it is returning the element ok, but unfortunately the Text() field is empty.
When I run a Jquery selector on the field:
$("#InputField").val() it gives me the text in the input field ok.
When I inspect the Html source, there is no value in the input field, and I guess this is because KnockoutJS is binding the value to the input field late.
How do I get WebDriver to pull the attributes, text etc. from the field correctly rather than just empty text?
Instead of using the Text property try using element.GetAttribute("value")
IWebElement element = _driver.FindElementById("InputField");
string value = element.GetAttribute("value");