Selenium Webdriver - tried to run command without establishing a connection

Viewed 2897

I'm new to selenium webdriver. I wrote a unittest script and ran it on 2 different windows 10 machines. On the first machine the code ran without any errors. When I run the code on the second machine, the code is executed and I can see that all the tests passed, but I still get the following error: "tried to run command without establishing a connection"

What is the reason for this, and how can I resolve this issue? Thx

1 Answers

Can I talk freely out of the sewing box? For Selenium I would recommend Linux (because you can crontab -e the following script), Google Chrome and chromedriver. It works very well, especially if you want to measure loading times. PhantomJS is sometimes seconds off and will not be further developed IIRC. Here's my untested example:

# -*- coding: utf-8 -*-

import datetime
import os
# from pushbullet import Pushbullet
import pytz
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# api_key = '<BLAHBLAHBLAH>'
# pb = Pushbullet(api_key)

d = pytz.timezone('Europe/Berlin').localize(datetime.datetime.now().replace(microsecond=0)).isoformat()
ok = True
ttfb = -1

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.binary_location = r'<PATH_TO_CHROME>'
webdriver = webdriver.Chrome(executable_path=r'<PATH_TO_CHROMEDRIVER>', chrome_options=chrome_options)

try:
    webdriver.get('https://www.example.com/')
    print(webdriver.execute_script('return performance'))
    ttfb = webdriver.execute_script('return (performance.timing.responseStart - performance.timing.navigationStart)')
    assert 'Example' in webdriver.find_element_by_css_selector("BODY").text
except (KeyboardInterrupt, MemoryError, SyntaxError, SystemExit):
    raise
except Exception as e:
    ok = False
    # push = pb.push_note('Something went wrong.', '<BLAHBLAH>' + '\n\Date: ' + d, channel=pb.get_channel('channel-has-to-be-created'))

try:
    webdriver.quit()
except (KeyboardInterrupt, MemoryError, SyntaxError, SystemExit):
    raise
except Exception as e:
    pass

print(d)
print(ok)
print(ttfb)

If anything is unclear, please ask. Whether this will help you with your Windows problem is up to you, but maybe you and others can compare their code and come to new conclusions. Have fun.

Related