0

I have a python selenium script to update date from datefield.

script = """
var date = 'input[id="date"]';
return date.value = {};
""".format('2018-01-09')

value = self.driver.execute_script(script)

But how do I verify this works in browser console. I'm using Safari.

In the console I can run document.getElementById('date').value = '2018-01-09' to change the date. But how can I verify execute_script works in console. I tried this in cosnsole but doesn't work

$(function($){
    var date = 'input[id="date"]';
    return date.value = '2018-01-09';
});

2 Answers 2

1

In generally, if the javascript snippet can work in browser console, it should be also work in execute_script(). The only possiblility can lead failure is the arguments we passed down javascript snippet from program script side.

Let's take following example to explain, suspect we want to change the value of inputbox, we can use execuite_script() as below to archive that:

inputEle = driver.find_element_by_xxxx('css/xpath locator')
newValue = '';
driver.execute_script('arguments[0].value=arguments[1]', inputEle, newValue)

In above example, there are two arguments passed down to javascript snippet from program script side:
1) inputEle will replace the placehold: arguments[0]
2) newValue will replace the placehold: arguemnts[1]

Only when not find the element inputEle, the execute_script() will fail. Once you make sure the arguments[0].value=arguments[1] is a correct javascript code snippet in browser, there is no other factor will cause execute_script() fail.

And we can using following manual steps to confirm the execute_script() can archive the effect we wanted or not before execute it:

  1. Verify the css/xpath locator in browser DevTool Element Tab

    As below picture shows, the matched element HTML code will be highlight with yellow background, then mouse over on the HTML code, the corresponding element on page will be highlight, until now we can confirm the css/xpath locator can find element we want or not. enter image description here

  2. Verify javascript snippet in browser DevTool Console Tab

    a) Select the HTML node of the element then switch to Console Tab
    b) Type $0 and press Enter, it will print out the HTML code of selected element
    c) Double check the output HTML is same as the selected element
    d) Type $0.value= and press Enter
    e) Back to the page to check any changes on page and it's you wanted.
    (In Chrome DevTool, $0 represent the selected HTML node in Element Tab)

    enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

You would have to run the python script and dump the output for script. Then take that and run it in the browser to see if it would work... or you could just run the script, maybe add some breakpoints to see variables during runtime, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.