Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/firefox/firefox_binary.py
1864 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
19
import os
20
import sys
21
import time
22
from subprocess import DEVNULL, STDOUT, Popen
23
24
from typing_extensions import deprecated
25
26
from selenium.common.exceptions import WebDriverException
27
from selenium.webdriver.common import utils
28
29
30
@deprecated("Use binary_location property in Firefox Options to set location")
31
class FirefoxBinary:
32
NO_FOCUS_LIBRARY_NAME = "x_ignore_nofocus.so"
33
34
def __init__(self, firefox_path=None, log_file=None):
35
"""Creates a new instance of Firefox binary.
36
37
:Args:
38
- firefox_path - Path to the Firefox executable. By default, it will be detected from the standard locations.
39
- log_file - A file object to redirect the firefox process output to. It can be sys.stdout.
40
Please note that with parallel run the output won't be synchronous.
41
By default, it will be redirected to /dev/null.
42
"""
43
self._start_cmd = firefox_path
44
# We used to default to subprocess.PIPE instead of /dev/null, but after
45
# a while the pipe would fill up and Firefox would freeze.
46
self._log_file = log_file or DEVNULL
47
self.command_line = None
48
self.platform = sys.platform
49
if not self._start_cmd:
50
self._start_cmd = self._get_firefox_start_cmd()
51
if not self._start_cmd.strip():
52
raise WebDriverException(
53
"Failed to find firefox binary. You can set it by specifying "
54
"the path to 'firefox_binary':\n\nfrom "
55
"selenium.webdriver.firefox.firefox_binary import "
56
"FirefoxBinary\n\nbinary = "
57
"FirefoxBinary('/path/to/binary')\ndriver = "
58
"webdriver.Firefox(firefox_binary=binary)"
59
)
60
# Rather than modifying the environment of the calling Python process
61
# copy it and modify as needed.
62
self._firefox_env = os.environ.copy()
63
self._firefox_env["MOZ_CRASHREPORTER_DISABLE"] = "1"
64
self._firefox_env["MOZ_NO_REMOTE"] = "1"
65
self._firefox_env["NO_EM_RESTART"] = "1"
66
67
def add_command_line_options(self, *args):
68
self.command_line = args
69
70
def launch_browser(self, profile, timeout=30):
71
"""Launches the browser for the given profile name.
72
73
It is assumed the profile already exists.
74
"""
75
self.profile = profile
76
77
self._start_from_profile_path(self.profile.path)
78
self._wait_until_connectable(timeout=timeout)
79
80
def kill(self):
81
"""Kill the browser.
82
83
This is useful when the browser is stuck.
84
"""
85
if self.process:
86
self.process.kill()
87
self.process.wait()
88
89
def _start_from_profile_path(self, path):
90
self._firefox_env["XRE_PROFILE_PATH"] = path
91
92
if self.platform == "linux":
93
self._modify_link_library_path()
94
command = [self._start_cmd, "-foreground"]
95
if self.command_line:
96
for cli in self.command_line:
97
command.append(cli)
98
self.process = Popen(command, stdout=self._log_file, stderr=STDOUT, env=self._firefox_env)
99
100
def _wait_until_connectable(self, timeout=30):
101
"""Blocks until the extension is connectable in the firefox."""
102
count = 0
103
while not utils.is_connectable(self.profile.port):
104
if self.process.poll():
105
# Browser has exited
106
raise WebDriverException(
107
"The browser appears to have exited "
108
"before we could connect. If you specified a log_file in "
109
"the FirefoxBinary constructor, check it for details."
110
)
111
if count >= timeout:
112
self.kill()
113
raise WebDriverException(
114
"Can't load the profile. Possible firefox version mismatch. "
115
"You must use GeckoDriver instead for Firefox 48+. Profile "
116
f"Dir: {self.profile.path} If you specified a log_file in the "
117
"FirefoxBinary constructor, check it for details."
118
)
119
count += 1
120
time.sleep(1)
121
return True
122
123
def _find_exe_in_registry(self):
124
try:
125
from _winreg import HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, OpenKey, QueryValue
126
except ImportError:
127
from winreg import HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, OpenKey, QueryValue
128
import shlex
129
130
keys = (
131
r"SOFTWARE\Classes\FirefoxHTML\shell\open\command",
132
r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command",
133
)
134
command = ""
135
for path in keys:
136
try:
137
key = OpenKey(HKEY_LOCAL_MACHINE, path)
138
command = QueryValue(key, "")
139
break
140
except OSError:
141
try:
142
key = OpenKey(HKEY_CURRENT_USER, path)
143
command = QueryValue(key, "")
144
break
145
except OSError:
146
pass
147
else:
148
return ""
149
150
if not command:
151
return ""
152
153
return shlex.split(command)[0]
154
155
def _get_firefox_start_cmd(self):
156
"""Return the command to start firefox."""
157
start_cmd = ""
158
if self.platform == "darwin": # small darwin due to lower() in self.platform
159
ffname = "firefox"
160
start_cmd = self.which(ffname)
161
# use hardcoded path if nothing else was found by which()
162
if not start_cmd:
163
start_cmd = "/Applications/Firefox.app/Contents/MacOS/firefox"
164
# fallback to homebrew installation for mac users
165
if not os.path.exists(start_cmd):
166
start_cmd = os.path.expanduser("~") + start_cmd
167
elif self.platform == "windows": # same
168
start_cmd = self._find_exe_in_registry() or self._default_windows_location()
169
elif self.platform == "java" and os.name == "nt":
170
start_cmd = self._default_windows_location()
171
else:
172
for ffname in ["firefox", "iceweasel"]:
173
start_cmd = self.which(ffname)
174
if start_cmd:
175
break
176
else:
177
# couldn't find firefox on the system path
178
raise RuntimeError(
179
"Could not find firefox in your system PATH."
180
" Please specify the firefox binary location or install firefox"
181
)
182
return start_cmd
183
184
def _default_windows_location(self):
185
program_files = [
186
os.getenv("PROGRAMFILES", r"C:\Program Files"),
187
os.getenv("PROGRAMFILES(X86)", r"C:\Program Files (x86)"),
188
]
189
for path in program_files:
190
binary_path = os.path.join(path, r"Mozilla Firefox\firefox.exe")
191
if os.access(binary_path, os.X_OK):
192
return binary_path
193
return ""
194
195
def _modify_link_library_path(self):
196
existing_ld_lib_path = os.environ.get("LD_LIBRARY_PATH", "")
197
198
new_ld_lib_path = self._extract_and_check(self.profile, "x86", "amd64")
199
200
new_ld_lib_path += existing_ld_lib_path
201
202
self._firefox_env["LD_LIBRARY_PATH"] = new_ld_lib_path
203
self._firefox_env["LD_PRELOAD"] = self.NO_FOCUS_LIBRARY_NAME
204
205
def _extract_and_check(self, profile, x86, amd64):
206
paths = [x86, amd64]
207
built_path = ""
208
for path in paths:
209
library_path = os.path.join(profile.path, path)
210
if not os.path.exists(library_path):
211
os.makedirs(library_path)
212
import shutil
213
214
shutil.copy(os.path.join(os.path.dirname(__file__), path, self.NO_FOCUS_LIBRARY_NAME), library_path)
215
built_path += library_path + ":"
216
217
return built_path
218
219
def which(self, fname):
220
"""Returns the fully qualified path by searching Path of the given
221
name."""
222
for pe in os.environ["PATH"].split(os.pathsep):
223
checkname = os.path.join(pe, fname)
224
if os.access(checkname, os.X_OK) and not os.path.isdir(checkname):
225
return checkname
226
return None
227
228