-1

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); ```    
cruisepandey
  • 28,520
  • 6
  • 20
  • 38

2 Answers2

0

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");
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

Generally, we should not use JavaScript clicks instead of WebDriver click until there is no other way since in most cases WebDriver click imitates user actions, so if you can't do something with WebDriver user is not able to do this via the GUI too.
Also, you are using the Select not in the right way.
Try this:

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

More details about using Select can be found here and in many other places.
Also, I'd add a wait before executing this command so the whole code would like this

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("capture-country")));
Select se = new Select(driver.findElement(By.id("capture-country"))); 
se.selectByValue("RUS");
Prophet
  • 32,350
  • 22
  • 54
  • 79