0

I entered one value in text field, using this code

driver.findelement(By.Id("Text"+Keys.enter));

Then, I used same id to enter second value in same text field

driver.findelement(By.id("Text2"+Keys.enter));

I think this is not the correct way to enter second value. Can I get an alternate way to do this? please!

5
  • 1
    Is it actually inserting Text into a textbox on this first one? I don't see how, personally.
    – Paul Muir
    Commented Aug 3, 2016 at 13:10
  • does your code even compile? could you please give more context code for this issue?
    – Malachi
    Commented Aug 3, 2016 at 15:43
  • yes, its taking first value Commented Aug 4, 2016 at 4:25
  • how i can enter second value in same field with same id? Commented Aug 4, 2016 at 5:33
  • May be you can enter both text once itself. Don't you? Suggestion - You need to add details for your question if you are really need help. Due to lack of info closing this question Commented Aug 11, 2016 at 9:09

2 Answers 2

1

So, time for some Selenium basics. I will try and be thorough so others can use this as time goes by.

When learning Selenium and Development it is important to understand what a method is doing. It's important to understand what you're code is doing. Let's assume you have you're WebDriver up and running and that you are in a place to actually call methods on it.

IWebDriver

  1. GoToUrl

    • This method navigates the browser to a URL. Pretty simple.

      IWebDriver driver = new InternetExplorerDriver();
      driver.Navigate().GoToUrl("www.google.com");
      
  2. FindElement

    • This method finds a single element on the current page.
    • If there is more than one matching element it will return the first element.
    • Returns an instance of IWebElement.
    • If no elements are found, throws an exception

      IWebElement searchField = driver.FindElement(By.Name("q"));
      
  3. FindElements

    • Much like FindElement but returns multiple IWebElement
    • Returns as an IList<IWebElement>
    • Does not throw an exception if no matching Elements

      IList<IWebElement> elements = driver.FindElements(By.Name("q"));
      

By

  1. Allows a user to search for elements
    • By.ClassName finds the element by it's class
    • By.CssSelector uses a CSS selector to find it. I typically use this method when ID is not available
    • By.LinkText uses the text of a Link
    • By.Name uses the Name of the element
    • By.PartialLinkText uses a partial match of a link text
    • By.TagName uses a match based on the HTML tag
    • By.XPath uses an XPath matcher

IWebElement

  1. SendKeys

    • Sends keys into the element.

      searchField.SendKeys("Selenium Tutorial");
      
  2. Clear

    • Clears out the text in the field

      searchField.Clear();
      
  3. Click

    • Clicks on the element

      searchField.Click();
      

And to compile together to do something:

IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("www.google.com");
IWebElement searchField = driver.FindElement(By.Name("q"));
searchField.SendKeys("Testing");
searchField.Clear();
searchField.SendKeys("Selenium Tutorial");
searchField.Submit();
2
  • 2
    this would be great for the documentation site, but I don't think this is going to help the OP, I think they need to give us a little more information, or as you put it, go back to the basics
    – Malachi
    Commented Aug 3, 2016 at 15:46
  • @Malachi I agree with your assessment, especially about needing more information. Hopefully this will assist the OP in understanding the differences between FindElement and SendKeys (as I believe the OP is trying to SendKeys with FindElement function) and assist in the questions that they will be having very soon.
    – Paul Muir
    Commented Aug 3, 2016 at 15:52
1
driver.findelement(By.Id("Text"+Keys.enter));

This piece of code is finding the element on the page with an ID of "text" + Keys.enter (whatever Keys.enter translates to in the compiler).

This line of code Does Not enter text into the text attribute of the element. You need to first Find the element, then send the value to the element, something like this

var element = driver.FindElement(By.Id("ElementID"));
element.SendKeys("text" + Keys.enter);

Or you can do it all in one line without assigning the element to a variable.

driver.FindElement(By.Id("ElementID")).SendKeys("text" + Keys.enter);

You can do it like this the first time. If you want to replace what is in the textbox then you need to do something different.

When I want to replace the text I could figure out how to delete what I know is in there, but that could turn ugly, so instead I make use of the Driver's ExecuteScript method like this

var element = driver.FindElement(By.Id("ElementID"));
var text = "text you want in your element's value attribute";
driver.ExecuteScript("arguments[0].setAttribute('value', arguments[1])", element, text);

The FindElement method just returns an IWebElement based on the search parameters that you give it.

I don't think that you want to find the element with the ID of "text" + Keys.enter, Keys.enter returns a key code for the enter key, not a function or an action.


You are not grabbing the same element. you are giving each command a different ID so they are looking for two different elements.

the first line

driver.findelement(By.Id("Text"+Keys.enter));

has a different ID than the second line

driver.findelement(By.id("Text2"+Keys.enter));

so when you put these into a variable they are actually two different elements

var firstElement = driver.FindElement(By.Id("id1"));
var secondElement = driver.FindElement(By.Id("id2"));
var areElementsTheSame = firstElement == secondElement;

areElementsTheSame is false.

you haven't put anything into any text field on the page when you run either line of code that you have posted, that is a different operation.

5
  • as you told id's are not different they are same. I want to enter other value in the same field with same id. so how can i do this Commented Aug 4, 2016 at 4:28
  • The id's in your code are different. you are passing different ID's from one line of code to the next. they are NOT the same in your code you posted. I have edited my answer to show you a way of doing what you are asking.
    – Malachi
    Commented Aug 4, 2016 at 11:55
  • Answer you told after entering first value you are giving clear code and then you entering the new value in the same field. Yes that is true, and its scenario we can do but, I want to enter the new value in same field without clearing the first value. Commented Aug 5, 2016 at 4:11
  • Why do you want to do it that way, @santhoshnayak?
    – Malachi
    Commented Aug 5, 2016 at 11:02
  • @santhoshnayak, I rolled back my most recent edit, because it actually overwrites the text in the element's text attribute. but this isn't clicking in the box and backspacing (or deleting) the text that is attribute. I am still unclear of the specifications, I think you should try to better explain your situation, in your post.
    – Malachi
    Commented Aug 5, 2016 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.