Search code examples
javaseleniumtextbox

Java Selenium textbox text not sticking


I already posted, but didn't really get an answer to my question. I am working on a personal project using Selenium in java to create an account. I am currently having an issue using send keys, so instead am doing a JavascriptExecutor. For some reason though, the JavascriptExecutor can't get the text to stay in the text box. There is an inactive textbox, which I click on then send the text to the active one, but it disappears immediately. Here is the HTML.

 <input name="dob-plain" class="step__input" type="text" placeholder="Date of Birth (mm / dd / yyyy)" autocomplete="off"> <input name="dob-format" type="hidden" value="MDY"> <span class="step__field__indicator"></span> </div>
<div class="step__field--date step__form__block" id="dob-field-active"> 
<input name="dob-month" class="step__input--date--mm" type="number" placeholder="mm" min="1" max="12" data-maxlength="2" value="" autocomplete="bday-month"> 
<span class="step__input--date-separator">/</span> 
<input name="dob-day" class="step__input--date--dd" type="number" placeholder="dd" min="1" max="31" data-maxlength="2" value="" autocomplete="bday-day"> 
<span class="step__input--date-separator">/</span> 
<input name="dob-year" class="step__input--date--yyyy" type="number" placeholder="yyyy" min="1900" max="2021" data-maxlength="4" value="" autocomplete="bday-year"> 
<span class="step__field__indicator"></span> </div>

Here is my code.

//Initiating your chromedriver
WebDriver driver = new ChromeDriver();
Actions action = new Actions(driver);
//Applied wait time
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//maximize window
driver.manage().window().maximize();

//open browser with desried URL
driver.get("https://account.battle.net/creation/flow/creation-full");

Select se = new Select(driver.findElement(By.id("capture-country"))); 

se.selectByVisibleText("Russian Federation");
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; 


//find the element in selenium webdriver
driver.findElement(By.xpath("//input[@name='dob-plain']")).click();

WebElement monthElm = driver.findElement(By.xpath("//input[@name='dob-month']"));
jsExecutor.executeScript("arguments[0].value ='01'", monthElm);
                
WebElement dateElm = driver.findElement(By.xpath("//input[@name='dob-day']"));
jsExecutor.executeScript("arguments[0].value ='01'", dateElm);
        
WebElement yearElm = driver.findElement(By.xpath("//input[@name='dob-year']"));
jsExecutor.executeScript("arguments[0].value ='2021'", yearElm); ```    

Solution

  • Instead of JavascriptExecutor, I would suggest you to use selectByValue, The reason is you can not use selectByVisibleText cause Russian Federation is not actually visible to selenium.

    The below code should work :

    Select se = new Select(driver.findElement(By.id("capture-country"))); 
    se.selectByValue("RUS");