-1

selenium.common.exceptions.WebDriverException: Message: unknown error: url is not defined

Code:

driver = webdriver.Chrome()
driver.get('https://google.com')
url='https://www.yahoo.com'
current = driver.current_window_handle
driver.execute_script("window.open(url);") #New tab
new_window = [window for window in driver.window_handles if window != current][0] # Get new tab ID
driver.switch_to.window(new_window) # Switch to new tab

While running the above code it gave me error:

selenium.common.exceptions.WebDriverException: Message: unknown error: url is not defined

Though url is define just 2 lines before..

Community
  • 1
  • 1
Grass
  • 185
  • 2
  • 14
  • And which line raised this error? Please provide full traceback. – Nouman Sep 08 '18 at 18:39
  • 1
    The error is because the **javascript** variable `url` is not defined. How would the browser's js engine know anything about your **python** variables? – DeepSpace Sep 08 '18 at 18:40
  • @DeepSpace this is written in Python – Grass Sep 08 '18 at 18:41
  • @Grass `driver.execute_script` executes javascript code using the browser's js engine. It has no idea what Python variables are defined – DeepSpace Sep 08 '18 at 18:42
  • @BlackThunder error raised on `driver.execute_script("window.open(url);") #New tab` – Grass Sep 08 '18 at 18:42
  • @DeepSpace So, how to define them? – Grass Sep 08 '18 at 18:43
  • @Grass See my answer – DeepSpace Sep 08 '18 at 18:45
  • Try: `driver.execute_script("window.open("+url+");")` – Nouman Sep 08 '18 at 18:45
  • @BlackThunder - Can you recheck your comment are double quotes correct ? – Amit Jain Sep 08 '18 at 18:48
  • @BlackThunder Got error: `selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.evaluate threw exception: SyntaxError: missing ) after argument list` with your codes too..(used updated one..) – Grass Sep 08 '18 at 18:52
  • 1
    @BlackThunder your code should be `driver.execute_script("window.open('"+url+"');")`, Grass this will work for you... – Chirag Jain Sep 08 '18 at 18:58
  • Whenever I open chrome from the selenium it also opens one tab ([see image](https://imgur.com/DfVcjya)), why it opens? How to stop that? (for the same codes..) @BlackThunder and all... – Grass Sep 08 '18 at 19:03
  • Can anyone please help me with: 1) [Chrome opens with “Data;” with selenium chromedriver](https://stackoverflow.com/questions/52243080/chrome-opens-with-data-with-selenium-chromedriver) and 2) [Console Log/ cmd.exe not closing in chromedriver](https://stackoverflow.com/questions/52236941/console-log-cmd-exe-not-closing-in-chromedriver) – Grass Sep 10 '18 at 17:49

2 Answers2

4

The error is because the javascript variable url is not defined.

driver.execute_script executes JS code using the browser's JS engine. It has no idea what Python variables are defined before execute_script was called.

Instead of hardcoding url you should use it as a variable:

driver.execute_script("window.open('{}');".format(url))
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • Got Error: `selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.evaluate threw exception: SyntaxError: missing ) after argument list` – Grass Sep 08 '18 at 18:52
  • 1
    I forgot `' '`. I fixed my answer. – DeepSpace Sep 08 '18 at 18:56
  • @DeepSpace - I also thinks it will work now. we need single quotes before '{ }' – Amit Jain Sep 08 '18 at 18:57
  • Whenever I open chrome from the selenium it also opens one tab ([see image](https://imgur.com/DfVcjya)), why it opens? How to stop that? (for the same codes..) – Grass Sep 08 '18 at 19:02
4

You need to pass Python variable to JavaScript. Try below:

driver.execute_script("window.open('%s');" % url)
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • 1
    I've posted my answer before you made an edit. Your solution was incorrect. Also String substitution via `%s` works bit faster then via formatting... Of course it can hardly be noticed in this case :) – Andersson Sep 08 '18 at 19:03
  • I never knew `%s` is faster than `{}`!! Thanks for the information sir. – SIM Sep 09 '18 at 15:15
  • @Topto , you can do `from timeit import timeit` and check how much time spend for `timeit('"some {} text".format("more")')` and for `timeit('"some %s text" % "more"')`... It's not a most precise way to check performance, but I got x20 faster result for `%s` – Andersson Sep 09 '18 at 15:38