0

I have a task I am attempting to automate in selenium using Python, which includes selecting a date. The date selection on the website has an input box that when you select drops down a pretty standard date table.

The input "text" box is set to read-only and I am having some trouble getting Selenium to find the dates I am asking it to select, being able to simply send_keys of the date required to the input box would be a far easier implementation.

After some research, I found this solution in JS:

((JavascriptExecutor)driver).executeScript("arguments[0].value=arguments[1]", element, "my text to put in the value");

Is there a similar function I could use in Python that would achieve the same?

I am not that familiar with Javascript so my abilities don't allow me to convert this into Python.

Any help would be appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

The same function exists in selenium python and is accessible from the driver object:

driver.execute_script("arguments[0].value=arguments[1]", element, "my text to put in the value")

You say you don't know javascript, so a quick outline for you...

You're passing 3 variables to execute_script:

  • First one is the js to execute arguments[0].value=arguments[1]. This look for 2 input arguments. It sets the .value of the first input with the value of the second input
  • element is the next variable you pass in and is first js argument (argument[0]) and is your webelement you've already identified
  • "my text...blah" is the second argument (argument[1]) and is the string value you want to set

More info on this method and others can be found in python-selenium docs.

Execute script is covered as such:

execute_script(script, *args) Synchronously Executes JavaScript in the current window/frame.

Args: script: The JavaScript to execute. *args: Any applicable arguments for your JavaScript. Usage: driver.execute_script(‘return document.title;’)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RichEdwards
  • 3,423
  • 2
  • 6
  • 22
0

The Selenium client equivalent line of code will be:

driver.execute_script("arguments[0].value=arguments[1]", element, "my text to put in the value")

It is to be noted the JavaScript command part remains the same and only the method name varies as per the binding art, be it Java, Python, C#, etc.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352