Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self,showbrowser):
display = Display(visible=0, size=(1600, 1024))
display.start()
if not showbrowser:
os.environ['MOZ_HEADLESS'] = '1'
firefoxprofile = webdriver.FirefoxProfile()
firefoxprofile.set_preference("permissions.default.desktop-notification", 1)
firefoxprofile.set_preference("dom.webnotifications.enabled", 1)
firefoxprofile.set_preference("dom.push.enabled", 1)
self.driver = webdriver.Firefox(firefox_profile=firefoxprofile)
self.driver.implicitly_wait(15)
self.driver.delete_all_cookies()
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import requests
import random
import re
page_url = "http://dev.kdlapi.com/testproxy" # 要访问的目标网页
# API接口,返回格式为json
api_url = ""
# API接口返回的ip
proxy = requests.get(api_url).json()['data']['proxy_list']
ip, port = random.choice(proxy).split(":")
port = int(port)
profile = webdriver.FirefoxProfile()
# 不使用代理的协议,注释掉对应的选项即可
settings = {
'network.proxy.type': 1, # 0: 不使用代理;1: 手动配置代理
'network.proxy.http': ip,
'network.proxy.http_port': port,
'network.proxy.ssl': ip, # 如果是https, 需要开启
'network.proxy.ssl_port': port,
'network.proxy.socks': ip,
'network.proxy.socks_port': port,
# 'network.proxy.ftp': ip,
# 'network.proxy.ftp_port': port
}
for key, value in settings.items():
profile.set_preference(key, value)
profile.update_preferences()
# Loop through the four different configurations.
for browser in ['chrome', 'firefox']:
for mode in ['headless', 'graphical']:
# Set up the driver with the appropriate settings.
if browser == 'chrome':
options = webdriver.ChromeOptions()
if mode == 'headless':
options.add_argument('headless')
options.add_argument(f'load-extension={extension_directory}')
driver = webdriver.Chrome(chrome_options=options)
elif browser == 'firefox':
if mode == 'headless':
os.environ['MOZ_HEADLESS'] = '1'
elif mode == 'graphical':
os.environ.pop('MOZ_HEADLESS', None)
profile = webdriver.FirefoxProfile()
profile.add_extension(extension_directory)
driver = webdriver.Firefox(profile, firefox_binary=firefox_binary)
# Navigate to the test page and let the extension do its thing.
driver.get('https://intoli.com/blog/javascript-injection/test-page.html')
# Save the results as an image.
os.makedirs('img', exist_ok=True)
filename = os.path.join('img',
f'selenium-custom-web-extension-{browser}-{mode}-results.png')
driver.get_screenshot_as_file(filename)
print(f'Saved "{filename}".')
# Cleanup the driver before the next test.
driver.quit()
for file in os.listdir(binarys_path):
for br in browsers_names:
if br in file:
browsers_paths[br] = binarys_path + file
if browser not in browsers_paths.keys():
print('[###] No option for this browser [###]\n')
print('Your current options are: \n')
for b in browsers_paths.keys():
print('- ' + b)
print("\nIf you dont have any of them sorry for you =)..\n")
sys.exit(1)
if 'firefox' in browser:
if f['proxy']:
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks",
f['proxy'].split(':')[0])
profile.set_preference("network.proxy.socks_port",
int(f['proxy'].split(':')[1]))
profile.update_preferences()
try:
driver = webdriver.Firefox(firefox_profile=profile)
driver.wait = WebDriverWait(driver, 90)
return driver
except Exception as e:
print('[#] Error while executing geckodriver')
print('[#] Install geckodriver or upgrade it!')
print(str(e))
else:
try:
"""
Configures and launches Firefox to run the vectorizer with.
Sets headless mode, sets the download directory to the desired output
directory, turns off page caching, and installs FathomFox and Fathom
Trainees.
We return the webdriver object, and the process IDs for both the Firefox
process and the geckodriver process so we can shutdown either
gracefully or ungracefully.
"""
print('Configuring Firefox...', end='', flush=True)
options = webdriver.FirefoxOptions()
options.headless = not show_browser
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.dir', str(pathlib.Path(output_directory).absolute()))
profile.set_preference('browser.cache.disk.enable', False)
profile.set_preference('browser.cache.memory.enable', False)
profile.set_preference('browser.cache.offline.enable', False)
firefox = webdriver.Firefox(
options=options,
firefox_profile=profile,
service_log_path=f'{temp_dir.name}/geckodriver.log',
)
firefox.install_addon(fathom_fox, temporary=True)
firefox.install_addon(fathom_trainees, temporary=True)
print('Done')
return firefox, firefox.capabilities['moz:processID'], firefox.service.process.pid
try:
# Get new browser instance and put it into browser array. One browser for one thread.
if browserString == '*chrome':
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--start-maximized')
chromeOptions.add_argument('--log-path=' + self.workDir + '/browser_drivers/chromedriver.log')
os.chdir(self.workDir + '/browser_drivers')
self.browsers.append(webdriver.Chrome(executable_path=self.workDir + '/browser_drivers/chromedriver.exe',
chrome_options=chromeOptions))
os.chdir(self.workDir)
elif browserString == '*ie':
self.browsers.append(webdriver.Ie(executable_path=self.workDir + '/browser_drivers/IEDriverServer.exe',
log_file=self.workDir + '/browser_drivers/iedriver.log'))
self.browsers[len(self.browsers) - 1].maximize_window()
else:
ffp = webdriver.FirefoxProfile(ffProfile)
self.browsers.append(webdriver.Firefox(firefox_profile=ffp, timeout=opTimeout))
self.browsers[len(self.browsers) - 1].maximize_window()
except BaseException:
traceback.print_exc()
return 1
return 0
def webdriver_firefox():
try:
f = open(os.environ['HOME'] + os.sep + '.selenium_port', 'r')
port = f.read()
f.close()
driver = webdriver.Remote(command_executor='http://127.0.0.1:%s/hub' % port, desired_capabilities=DesiredCapabilities.FIREFOX)
except:
default_profile = filter(lambda s: s.find('.selenium') >= 0, os.listdir(firefox_profiles_path))[0]
profile = webdriver.FirefoxProfile(firefox_profiles_path + os.sep + default_profile)
driver = webdriver.Firefox(profile)
f = open(os.environ['HOME'] + os.sep + '.selenium_port', 'w')
f.write(str(profile.port))
f.close()
return driver
# set Firefox Agent to mobile agent
user_agent = (
"Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X) AppleWebKit/605.1.15 "
"(KHTML, like Gecko) FxiOS/18.1 Mobile/16B92 Safari/605.1.15"
)
# keep user_agent
Settings.user_agent = user_agent
firefox_options = Firefox_Options()
if headless_browser:
firefox_options.add_argument("-headless")
if browser_profile_path is not None:
firefox_profile = webdriver.FirefoxProfile(browser_profile_path)
else:
firefox_profile = webdriver.FirefoxProfile()
# set English language
firefox_profile.set_preference("intl.accept_languages", "en-US")
firefox_profile.set_preference("general.useragent.override", user_agent)
if disable_image_load:
# permissions.default.image = 2: Disable images load,
# this setting can improve pageload & save bandwidth
firefox_profile.set_preference("permissions.default.image", 2)
if proxy_address and proxy_port:
firefox_profile.set_preference("network.proxy.type", 1)
firefox_profile.set_preference("network.proxy.http", proxy_address)
firefox_profile.set_preference("network.proxy.http_port", int(proxy_port))
try:
# Get new browser instance and put it into browser array. One browser for one thread.
if browserString == '*chrome':
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--start-maximized')
chromeOptions.add_argument('--log-path=' + workDir + '/browser_drivers/chromedriver.log')
os.chdir(workDir + '/browser_drivers')
browsers.append(webdriver.Chrome(executable_path=workDir + '/browser_drivers/chromedriver.exe',
chrome_options=chromeOptions))
os.chdir(workDir)
elif browserString == '*ie':
browsers.append(webdriver.Ie(executable_path=workDir + '/browser_drivers/IEDriverServer.exe',
log_file=workDir + '/browser_drivers/iedriver.log'))
browsers[len(browsers) - 1].maximize_window()
else:
ffp = webdriver.FirefoxProfile(ffProfile)
browsers.append(webdriver.Firefox(firefox_profile=ffp, timeout=opTimeout))
browsers[len(browsers) - 1].maximize_window()
except BaseException:
traceback.print_exc()
return 1
return 0