Unable to retrieve Textbox Value In Selenium Testing - java

I am new to Selenium testing, I am trying to put the value into the textbox with the help of xpath, name & id. (java using eclipse IDE):
driver.findElement(By.xpath(".//* [#id='txtEmpDepAddressLine1']")).sendKeys("2nd Main")
driver.findElement(By.xpath(".//*[#id='txtEmpDepAddressLine2']")).sendKeys("Bangalore");
While running, textbox is not retrieve the from the above code. Can anyone help me out to solve this problem?

Use By.id instead By.xpath
If you insist xpath then:
Start with: // instead: .//
Make sure the id value is unique on page.
if using xpath in some browsers id or ID makes differnce.

It is difficult to say without the HTML, but I'd suggest a few things:
Open your page, go to the Google Chrome DevTools console, and verify you can locate the element using your xpath. So you'd type:
$x("//* [#id='txtEmpDepAddressLine1']")
and verify the text box is located.
If the sendKeys isn't working on the element at that id, I'd wonder if it is not on the text field itself, but on a containing table or something like that. Make sure the element located by the id is the input, so the sendKeys will work.

By
In the text box it is fetching the value but it can't able not store
in the text box field
do you mean you can see selenium typing those values into the textbox and then the values disappear?
If that is what is happening, it is possible there is some javascript running on the page while selenium is entering the values in the textbox. you can try to put a wait before selenium enters the value. If waiting resolves the issue, you will need to write a method to detect when the javascript is done.

Related

How can I create an XPath that will look for button text or text from a nested element within the button?

I am using Selenium web driver in order to test out a button and this button can either have text like so
<button>Foo</button>
Or it can look like this with nested tags of any type or depth.
<button><span>Foo</span></button>
I tried doing
Driver.findElement(By.xpath("//*[text()='Foo']"));
But this would only get the text in the immediate tag level. I added a “//*” to the xpath and it would work for the nested element but then stop working for when the text is in the immediate level. I am looking for kind of OR condition or something where I can get this to work on both variations.
I am using Java and selenium web driver.
Thanks in Advance,
Juan
Try this:
driver.findElement(By.xpath("//button[contains(.,'Foo')]"));
This will search for button element containing Foo text content in itself or any it child, exactly what you asking for.
BTW in Java we normally give objects names starting with non-capital letter, so it should be driver, not Driver there.

Send text to div in Selenium + Java

I want to send a text to a div element in web.whatsapp. I tried several ways ie using XPath, cssSelector, class name etc. but none is working for me.
This textbox comes when we attach an image in web.whatsapp.
Please step into the div and use the div with the class "selectable-text". As this is a contenteditable div you should be able to click into it and call then sendKeys() onto the element.
if this does not help please let me know then I will look in our code how we are filling such divs exactly.
You need to drill down further until you get to a web element that takes an input.

Any way to pass data from a HTML to a Java application?

I have a JavaScript code embedded in a Java method to retrieve properties of any element clicked upon in a web page.
That is, I navigate to a specific URL by creating an instance of Selenium WebDriver from my Java application and click on an object in the page that gets loaded. After the single click on the object, a HTML page opens listing the name, type, id, class name etc. associated to that object as a radio button group. I select one option from the list and click on OK. What I wanted to know is, can I push the selected data from the HTML back onto my Java application on the click event on the HTML?
Any help appreciated! Thanks! :)
WebElement interface of selenium Webdriver gives a method
getAttribute which provides value of a given attribute of the desired html element.
I hope this method is what you are looking for.
USAGE:
String field_value;
field_value = driver.findElement(By.xpath("xpath Locator")).getAttribute("attribute name")
The information which you want to get should be in any of the attribute associated to that element.
Similarly if you want all attributes associated with the element in one go you can use something like this :
((JavascriptExecutor)driver).executeScript("return arguments[0].attributes);", element);
The above will give you a comma separated list of all attributes on which you can apply your coding logic to separate them and get the corresponding values using loops.

Is it possible to enter a text in a textbox without specific Element ID

Im doing a test that enter text in the textbox but it dont have specific ID so everytime i run the test it will change. im using selenium webdriver in java please help
The following can work. You can google for them and see how they work.
driver.findElement(By.id("id"));
driver.findElement(By.cssSelector("cssSelector"));
driver.findElement(By.name("name"));
driver.findElement(By.linkText("linkText"));
driver.findElement(By.partialLinkText("partialLinkText"));
driver.findElement(By.className("className"));
driver.findElement(By.xpath("xpath"));
I am sure some of them will be useful. Please let me know if you want more info.
The easiness of using them is in the order which i have mentioned.
What means doesn't have a specific ID?If id at least partially remains the same you can use the CSS locator
driver.findElement(By.cssLocator("input[id*=somePartWhichNotChange]"));
//* star means contains
if not, then you can use cssSelector to get to your element like "body table input" or use xpath as a last resort.

Get the text out of an WebElement found by id update by jquery

I'm trying to get some text out of and id so i can use it later in the test. And i can find the element with:
WebElement jrnrText = driver.findElement(
By.id("ctl00_content_createnewschema_modalAlert_alertMessage"))
But the problem is that the value will get dynamically added later by jquery, so the id will be present when i get to the page. I then click a button and jquery will update the id.
I know i can use the jrnrText.getText() but that will not give me anything since the id don't contain anything before i click the button.
This is my code:
driver.findElement(By.id("ctl00_content_createnewschema_btnCreate")).click();
WebElement jrnrText = driver.findElement(
By.id("ctl00_content_createnewschema_modalAlert_alertMessage"))
if(jrnrText.isDisplayed()){
wait.until(presenceOfElementLocated(By.xpath('''//td[contains(text(),"part of the text"]''')))
}
println jrnrText.getText()
Your question is confusing, but I will try to answer it. First off, if an element's ID is dynamically changing based on user input then it is completely defeating the purpose of an ID. This is a bug that should be fixed in the web site.
If for some reason the bug can't be fixed then we would need more details. Where in the code that you show above does it fail? When you say "the id don't contain anything before i click the button" do you mean that the element with an id of "ctl00_content_createnewschema_modalAlert_alertMessage" does not exist? Even though it is occurring after the button click which you stated updates the ID?

Categories