0

When running the python script below I'm getting an error message "javascript error: missing ) after argument list". The script is failing at "driver.execute_script(tab)". I've tried adjusting it but I'm not able to solve it.

import selenium
from selenium import webdriver

driver = webdriver.Chrome()

driver.get('http://techstepacademy.com/training-ground')

list = ['http://yahoo.com', 'http://google.com','http://techstepacademy.com/training-ground']

for url in list:
    tab = "window.open(" + url + ",'_blank')"
    driver.execute_script(tab)
3
  • can you try: tab = "window.open({}, '_blank')".format(url) Commented Dec 7, 2019 at 0:07
  • 2
    Your code is dropping the URL into the JavaScript without any quotation marks. Commented Dec 7, 2019 at 0:07
  • 1
    Does this answer your question? Escape character removed when using selenium execute_script through python Commented Jan 11, 2021 at 4:23

2 Answers 2

2

You can avoid those quotes completely:

driver.execute_script("window.open(arguments[0])", url)
Sign up to request clarification or add additional context in comments.

Comments

0

add quotes around url.

 tab = "window.open(" + '"' + url + '"' + ",'_blank')"

1 Comment

This is very bad, because some JavaScript special character in string might appear in url, such as ` or "`.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.