0

I am trying to:

  • Clear an existing value in the text box (to be specific, this is a first name field)
  • Then save it with no value in the text box.

But somehow clear(); does not clears the existing value. It retains the existing value (first name).

Does anyone know how to fix this?

driver.findElement(By.id("PrimaryContact_FirstName_Value")).clear();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
JuneH
  • 1
  • 1
  • 1
    Can you provide us with the **relevant** html ? And Possibly the URL if available. – PixelEinstein Mar 29 '18 at 19:18
  • Thank you for your comment! Unfortunately I am not able to provide the things you asked for...I am sorry. I am not sure if I can share those here. Do you have any idea why this does not work? Or is it hard to tell without the html or the URL? – JuneH Mar 29 '18 at 19:26
  • 1
    It will be very hard to know what's going on without seeing the html. If you could just copy the full HTML of the element you are trying to clear that would greatly help. Without that I will try my best to answer blindly. – PixelEinstein Mar 29 '18 at 19:29
  • I am sorry but I cannot share the copy of the html for some personal reason...I would appreciate if you could share your guesses with me.. Thank you. – JuneH Mar 29 '18 at 19:40
  • There may be an issue with masking; try using .SendKeys(Keys.Control, "a").SendKeys(Keys.Backspace) to the control and see if that works – Rescis Mar 29 '18 at 19:52
  • Thank you for your comment, I tried it, but did not work. Thank you though. – JuneH Mar 29 '18 at 20:09
  • I am sorry for the late comment, but the below code worked! Thank you so much Rescis. .sendKeys(Keys.CONTROL,"a").sendKeys(Keys.BACK_SPACE); – JuneH Apr 06 '18 at 19:10

2 Answers2

0

Without knowing the context of the site/element, I will do my best to help you through this.

First let's try this to see if it clears the text:

JavascriptExecutor js =(JavascriptExecutor)driver;
WebElement element = driver.findElement(By.id("PrimaryContact_FirstName_Value"));
js.executeScript("arguments[0].value = ''", element));

EDIT:

Does it give you any errors? Does it look like this?

<form name="myForm" action="/action_page.php" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="button" onclick="formSubmit()" value="Send form data!">
</form>
PixelEinstein
  • 1,713
  • 1
  • 8
  • 17
  • I forgot to mention, I am using Java for this. I assume the code you shared works with JavaScript?? I am REALLY new to programming, so please correct me if I am mistaken, thank you for your help! – JuneH Mar 29 '18 at 20:18
  • It's okay, it is Java code, look at [THIS](https://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-java) for more information on executing js commands from java. The code below is asking if your html looks similar to that? – PixelEinstein Mar 29 '18 at 20:25
  • If your given locator `By.id("PrimaryContact_FirstName_Value")` indeed find the exact element, This solution should fix your problem. First of all, confirm your given locator is correct and the firstname box not inside frame – yong Mar 29 '18 at 23:24
  • Thank you for your comments, I don't have access to Selenium until next Thursday, I will leave a comment as soon as I try your solution. Thank you. – JuneH Mar 30 '18 at 20:38
  • Thank you everyone who gave me comments. This issue was solved with the code below. .sendKeys(Keys.CONTROL,"a").sendKeys(Keys.BACK_SPACE); – JuneH Apr 06 '18 at 19:12
0

As you mentioned this is a first name field it is highly possible you are trying to interact with the element before it is interactable. So you need to induce WebDriverWait for the element to be clickable.

As per your attempted code block you can use the following code block based on a xpath to clear the existing text :

WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='PrimaryContact_FirstName_Value']")));
myElement.click();
myElement.clear();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi DebanjanB, thank you for your comment. I don't have access to Selenium until next Thursday, I will leave a comment here as soon as I try the code! Thank you. – JuneH Mar 30 '18 at 20:36