2

I use Javascript to collate data from an HTML page via Selenium; however, I cannot run the Javascript portion from my computer via Selenium/py cause Python only offers the driver.execute_script('string of a script'), whereas I have a multi-line for loop which won't execute with these commands.

The loop:

for (let i = 0; i < Values.length; i++) {
    if (Values[i].getAttribute('automation-id') === 'contact-wealth-manager') {
        ele = Values[i]
    }
}

As you can test, this won't work with the standard "driver.execute_script("let value = '';")

1 Answer 1

0

To execute the multiline loop Javascript you need to pass the script as an argument to execute_script() method as follows:

driver.execute_script("""
    for (let i = 0; i < Values.length; i++) {
        if (Values[i].getAttribute('automation-id') === 'contact-wealth-manager') {
            ele = Values[i]
        }
    }
""")
Sign up to request clarification or add additional context in comments.

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.