1

I need to do some automatic tests within system. Some fields got validations, and it probably can't be done by just sendKeys (then I'm doing it, it just write some one string, not whole. Tried iterating sendKeys thru string, didn't work either)

Right now I'm trying to input value to field by javascript. Got something like it:

WebElement pesel = driver.findElement(fldPesel);
jse.executeScript("arguments[0].value='80120804076';", pesel);

But, I would want not to have value in executeScript, but java variable, so it would look and work better. And got some randomisation

How I could do that?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
M Zal
  • 31
  • 1
  • 6
  • 1
    When you write a value directly to an element using JS, you are short cutting whatever validations have been created on that input. You could cause all kinds of problems on that page or following pages. It would be better to find a way to enter the text using `.sendKeys()`. – JeffC Apr 01 '18 at 13:55

2 Answers2

2

As per your statement I would want not to have value in executeScript, but java variable you can reference the Sting value through a Java variable :

String myValue = "80120804076";
WebElement pesel = driver.findElement(fldPesel);
jse.executeScript("arguments[0].value='" + myValue + "';", pesel);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you! Could you give me advice, how i can run ajax on this field? Got in the field: onchange="A4J.AJAX.Submit('j_id259',event,{'control':this,'similarityGroupingId':'j_id259:j_id324:j_id333','parameters':{'j_id259:j_id324:j_id333':'j_id259:j_id324:j_id333','ajaxSingle':'j_id259:j_id324:kosEdycjaNazwisko1'} } ) " – M Zal Apr 03 '18 at 10:07
  • @MZal Can you raise a new question for your new requirement please? – undetected Selenium Apr 03 '18 at 10:08
0

You can use more than one arguments. You should also use setAttribute to do this

String value = "80120804076";
WebElement pesel = driver.findElement(fldPesel);
jse.executeScript("arguments[0].setAttribute('value', `arguments[1]');", pesel, value);
Guy
  • 46,488
  • 10
  • 44
  • 88