Best Python code snippet using playwright-python
webdrivertools.py
Source:webdrivertools.py
...103 defaul_caps = webdriver.DesiredCapabilities.CHROME.copy()104 desired_capabilities = self._remote_capabilities_resolver(desired_capabilities, defaul_caps)105 return self._remote(desired_capabilities, remote_url, options=options)106 if is_falsy(executable_path):107 executable_path = self._get_executable_path(webdriver.Chrome)108 return webdriver.Chrome(options=options, service_log_path=service_log_path, executable_path=executable_path,109 **desired_capabilities)110 def create_headless_chrome(self, desired_capabilities, remote_url, options=None, service_log_path=None,111 executable_path='chromedriver'):112 if not options:113 options = webdriver.ChromeOptions()114 options.headless = True115 return self.create_chrome(desired_capabilities, remote_url, options, service_log_path, executable_path)116 def _get_executable_path(self, webdriver):117 if PY3:118 signature = inspect.signature(webdriver.__init__)119 parameters = signature.parameters120 executable_path = parameters.get('executable_path')121 if not executable_path:122 return None123 return executable_path.default124 else: # TODO: Remove else when Python 2 is dropped.125 signature = inspect.getargspec(webdriver.__init__)126 if 'executable_path' in signature.args:127 index = signature.args.index('executable_path')128 return signature.defaults[index - 1]129 def create_firefox(self, desired_capabilities, remote_url, ff_profile_dir, options=None, service_log_path=None,130 executable_path='geckodriver'):131 profile = self._get_ff_profile(ff_profile_dir)132 if is_truthy(remote_url):133 default_caps = webdriver.DesiredCapabilities.FIREFOX.copy()134 desired_capabilities = self._remote_capabilities_resolver(desired_capabilities, default_caps)135 return self._remote(desired_capabilities, remote_url,136 profile, options)137 service_log_path = service_log_path if service_log_path else self._geckodriver_log138 if is_falsy(executable_path):139 executable_path = self._get_executable_path(webdriver.Firefox)140 return webdriver.Firefox(options=options, firefox_profile=profile,141 service_log_path=service_log_path, executable_path=executable_path,142 **desired_capabilities)143 def _get_ff_profile(self, ff_profile_dir):144 if isinstance(ff_profile_dir, FirefoxProfile):145 return ff_profile_dir146 if is_falsy(ff_profile_dir):147 return webdriver.FirefoxProfile()148 try:149 return webdriver.FirefoxProfile(ff_profile_dir)150 except (OSError, FileNotFoundError):151 ff_options = self.selenium_options._parse(ff_profile_dir)152 ff_profile = webdriver.FirefoxProfile()153 for option in ff_options:154 for key in option:155 attr = getattr(ff_profile, key)156 if callable(attr):157 attr(*option[key])158 else:159 setattr(ff_profile, key, *option[key])160 return ff_profile161 @property162 def _geckodriver_log(self):163 log_file = self._get_log_path(os.path.join(self.log_dir, 'geckodriver-{index}.log'))164 logger.info('Firefox driver log is always forced to to: %s' % log_file)165 return log_file166 def create_headless_firefox(self, desired_capabilities, remote_url, ff_profile_dir, options=None,167 service_log_path=None, executable_path='geckodriver'):168 if not options:169 options = webdriver.FirefoxOptions()170 options.headless = True171 return self.create_firefox(desired_capabilities, remote_url, ff_profile_dir, options, service_log_path,172 executable_path)173 def create_ie(self, desired_capabilities, remote_url, options=None, service_log_path=None,174 executable_path='IEDriverServer.exe'):175 if is_truthy(remote_url):176 defaul_caps = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()177 desired_capabilities = self._remote_capabilities_resolver(desired_capabilities, defaul_caps)178 return self._remote(desired_capabilities, remote_url, options=options)179 if is_falsy(executable_path):180 executable_path = self._get_executable_path(webdriver.Ie)181 return webdriver.Ie(options=options, service_log_path=service_log_path, executable_path=executable_path,182 **desired_capabilities)183 def _has_options(self, web_driver):184 signature = inspect.getargspec(web_driver.__init__)185 return 'options' in signature.args186 def create_edge(self, desired_capabilities, remote_url, options=None, service_log_path=None,187 executable_path='MicrosoftWebDriver.exe'):188 if is_truthy(remote_url):189 defaul_caps = webdriver.DesiredCapabilities.EDGE.copy()190 desired_capabilities = self._remote_capabilities_resolver(desired_capabilities, defaul_caps)191 return self._remote(desired_capabilities, remote_url)192 if is_falsy(executable_path):193 executable_path = self._get_executable_path(webdriver.Edge)194 if self._has_options(webdriver.Edge):195 # options is supported from Selenium 4.0 onwards196 # If can be removed when minimum Selenium version is 4.0 or greater197 return webdriver.Edge(options=options, service_log_path=service_log_path, executable_path=executable_path,198 **desired_capabilities)199 return webdriver.Edge(service_log_path=service_log_path, executable_path=executable_path, **desired_capabilities)200 def create_opera(self, desired_capabilities, remote_url, options=None, service_log_path=None,201 executable_path='operadriver'):202 if is_truthy(remote_url):203 defaul_caps = webdriver.DesiredCapabilities.OPERA.copy()204 desired_capabilities = self._remote_capabilities_resolver(desired_capabilities, defaul_caps)205 return self._remote(desired_capabilities, remote_url, options=options)206 if is_falsy(executable_path):207 executable_path = self._get_executable_path(webdriver.Opera)208 return webdriver.Opera(options=options, service_log_path=service_log_path, executable_path=executable_path,209 **desired_capabilities)210 def create_safari(self, desired_capabilities, remote_url, options=None, service_log_path=None,211 executable_path='/usr/bin/safaridriver'):212 if is_truthy(remote_url):213 defaul_caps = webdriver.DesiredCapabilities.SAFARI.copy()214 desired_capabilities = self._remote_capabilities_resolver(desired_capabilities, defaul_caps)215 return self._remote(desired_capabilities, remote_url)216 if options or service_log_path:217 logger.warn('Safari browser does not support Selenium options or service_log_path.')218 if is_falsy(executable_path):219 executable_path = self._get_executable_path(webdriver.Safari)220 return webdriver.Safari(executable_path=executable_path, **desired_capabilities)221 def create_phantomjs(self, desired_capabilities, remote_url, options=None, service_log_path=None,222 executable_path='phantomjs'):223 warnings.warn('SeleniumLibrary support for PhantomJS has been deprecated, '224 'please use headlesschrome or headlessfirefox instead.')225 if is_truthy(remote_url):226 defaul_caps = webdriver.DesiredCapabilities.PHANTOMJS.copy()227 desired_capabilities = self._remote_capabilities_resolver(desired_capabilities, defaul_caps)228 return self._remote(desired_capabilities, remote_url)229 if options:230 logger.warn('PhantomJS browser does not support Selenium options.')231 if is_falsy(executable_path):232 executable_path = self._get_executable_path(webdriver.PhantomJS)233 return webdriver.PhantomJS(service_log_path=service_log_path, executable_path=executable_path,234 **desired_capabilities)235 def create_htmlunit(self, desired_capabilities, remote_url, options=None, service_log_path=None,236 executable_path=None):237 if service_log_path or options or executable_path:238 logger.warn('Htmlunit does not support Selenium options, service_log_path or executable_path argument.')239 defaul_caps = webdriver.DesiredCapabilities.HTMLUNIT.copy()240 desired_capabilities = self._remote_capabilities_resolver(desired_capabilities, defaul_caps)241 return self._remote(desired_capabilities, remote_url, options=options)242 def create_htmlunit_with_js(self, desired_capabilities, remote_url, options=None, service_log_path=None,243 executable_path=None):244 if service_log_path or options or executable_path:245 logger.warn('Htmlunit with JS does not support Selenium options, service_log_path or executable_path argument.')246 defaul_caps = webdriver.DesiredCapabilities.HTMLUNITWITHJS.copy()...
patcher.py
Source:patcher.py
1#!/usr/bin/env python32# this module is part of undetected_chromedriver34import io5import logging6import os7import random8import re9import string10import sys11import zipfile12from distutils.version import LooseVersion13from urllib.request import urlopen, urlretrieve1415logger = logging.getLogger(__name__)1617IS_POSIX = sys.platform.startswith(("darwin", "cygwin", "linux"))181920class Patcher(object):21 url_repo = "https://chromedriver.storage.googleapis.com"22 zip_name = "chromedriver_%s.zip"23 exe_name = "chromedriver%s"2425 platform = sys.platform26 if platform.endswith("win32"):27 zip_name %= "win32"28 exe_name %= ".exe"29 if platform.endswith("linux"):30 zip_name %= "linux64"31 exe_name %= ""32 if platform.endswith("darwin"):33 zip_name %= "mac64"34 exe_name %= ""3536 if platform.endswith("win32"):37 d = "~/appdata/roaming/undetected_chromedriver"38 elif platform.startswith("linux"):39 d = "~/.local/share/undetected_chromedriver"40 elif platform.endswith("darwin"):41 d = "~/Library/Application Support/undetected_chromedriver"42 else:43 d = "~/.undetected_chromedriver"44 data_path = os.path.abspath(os.path.expanduser(d))4546 def __init__(self, executable_path=None, force=False, version_main: int = 0):47 """4849 Args:50 executable_path: None = automatic51 a full file path to the chromedriver executable52 force: False53 terminate processes which are holding lock54 version_main: 0 = auto55 specify main chrome version (rounded, ex: 82)56 """5758 self.force = force5960 if not executable_path:61 executable_path = os.path.join(self.data_path, self.exe_name)6263 if not IS_POSIX:64 if not executable_path[-4:] == ".exe":65 executable_path += ".exe"6667 self.zip_path = os.path.join(self.data_path, self.zip_name)6869 self.executable_path = os.path.abspath(os.path.join(".", executable_path))7071 self.version_main = version_main72 self.version_full = None737475 def auto(self, executable_path=None, force=False, version_main=None):76 """77 """78 if executable_path:79 self.executable_path = executable_path80 if version_main:81 self.version_main = version_main82 if force is True:83 self.force = force8485 try:86 os.unlink(self.executable_path)87 except PermissionError:88 if self.force:89 self.force_kill_instances(self.executable_path)90 return self.auto(force=not self.force)91 try:92 if self.is_binary_patched():93 # assumes already running AND patched94 return True95 except PermissionError:96 pass97 # return False98 except FileNotFoundError:99 pass100101 release = self.fetch_release_number()102 self.version_main = release.version[0]103 self.version_full = release104 self.unzip_package(self.fetch_package())105 # i.patch()106 return self.patch()107108 def patch(self):109 self.patch_exe()110 return self.is_binary_patched()111112 def fetch_release_number(self):113 """114 Gets the latest major version available, or the latest major version of self.target_version if set explicitly.115 :return: version string116 :rtype: LooseVersion117 """118 path = "/latest_release"119 if self.version_main:120 path += f"_{self.version_main}"121 path = path.upper()122 logger.debug("getting release number from %s" % path)123 return LooseVersion(urlopen(self.url_repo + path).read().decode())124125 def parse_exe_version(self):126 with io.open(self.executable_path, "rb") as f:127 for line in iter(lambda: f.readline(), b""):128 match = re.search(br"platform_handle\x00content\x00([0-9.]*)", line)129 if match:130 return LooseVersion(match[1].decode())131132 def fetch_package(self):133 """134 Downloads ChromeDriver from source135136 :return: path to downloaded file137 """138 u = "%s/%s/%s" % (self.url_repo, self.version_full.vstring, self.zip_name)139 logger.debug("downloading from %s" % u)140 # return urlretrieve(u, filename=self.data_path)[0]141 return urlretrieve(u)[0]142143 def unzip_package(self, fp):144 """145 Does what it says146147 :return: path to unpacked executable148 """149 logger.debug("unzipping %s" % fp)150 try:151 os.unlink(self.zip_path)152 except (FileNotFoundError, OSError):153 pass154155 os.makedirs(self.data_path, mode=0o755, exist_ok=True)156157 with zipfile.ZipFile(fp, mode="r") as zf:158 zf.extract(self.exe_name, os.path.dirname(self.executable_path))159 os.remove(fp)160 os.chmod(self.executable_path, 0o755)161 return self.executable_path162163 @staticmethod164 def force_kill_instances(exe_name):165 """166 kills running instances.167 :param: executable name to kill, may be a path as well168169 :return: True on success else False170 """171 exe_name = os.path.basename(exe_name)172 if IS_POSIX:173 r = os.system("kill -f -9 $(pidof %s)" % exe_name)174 else:175 r = os.system("taskkill /f /im %s" % exe_name)176 return not r177178 @staticmethod179 def gen_random_cdc():180 cdc = random.choices(string.ascii_lowercase, k=26)181 cdc[-6:-4] = map(str.upper, cdc[-6:-4])182 cdc[2] = cdc[0]183 cdc[3] = "_"184 return "".join(cdc).encode()185186 def is_binary_patched(self, executable_path=None):187 """simple check if executable is patched.188189 :return: False if not patched, else True190 """191 executable_path = executable_path or self.executable_path192 with io.open(executable_path, "rb") as fh:193 for line in iter(lambda: fh.readline(), b""):194 if b"cdc_" in line:195 return False196 else:197 return True198199 def patch_exe(self):200 """201 Patches the ChromeDriver binary202203 :return: False on failure, binary name on success204 """205 logger.info("patching driver executable %s" % self.executable_path)206207 linect = 0208 replacement = self.gen_random_cdc()209 with io.open(self.executable_path, "r+b") as fh:210 for line in iter(lambda: fh.readline(), b""):211 if b"cdc_" in line:212 fh.seek(-len(line), 1)213 newline = re.sub(b"cdc_.{22}", replacement, line)214 fh.write(newline)215 linect += 1216 return linect217218 def __repr__(self):219 return "{0:s}({1:s})".format(220 self.__class__.__name__,221 self.executable_path,
...
dyld.py
Source:dyld.py
1"""2dyld emulation3"""4import os5from framework import framework_info6from dylib import dylib_info7from itertools import *8__all__ = [9 'dyld_find', 'framework_find',10 'framework_info', 'dylib_info',11]12# These are the defaults as per man dyld(1)13#14DEFAULT_FRAMEWORK_FALLBACK = [15 os.path.expanduser("~/Library/Frameworks"),16 "/Library/Frameworks",17 "/Network/Library/Frameworks",18 "/System/Library/Frameworks",19]20DEFAULT_LIBRARY_FALLBACK = [21 os.path.expanduser("~/lib"),22 "/usr/local/lib",23 "/lib",24 "/usr/lib",25]26def ensure_utf8(s):27 """Not all of PyObjC and Python understand unicode paths very well yet"""28 if isinstance(s, unicode):29 return s.encode('utf8')30 return s31def dyld_env(env, var):32 if env is None:33 env = os.environ34 rval = env.get(var)35 if rval is None:36 return []37 return rval.split(':')38def dyld_image_suffix(env=None):39 if env is None:40 env = os.environ41 return env.get('DYLD_IMAGE_SUFFIX')42def dyld_framework_path(env=None):43 return dyld_env(env, 'DYLD_FRAMEWORK_PATH')44def dyld_library_path(env=None):45 return dyld_env(env, 'DYLD_LIBRARY_PATH')46def dyld_fallback_framework_path(env=None):47 return dyld_env(env, 'DYLD_FALLBACK_FRAMEWORK_PATH')48def dyld_fallback_library_path(env=None):49 return dyld_env(env, 'DYLD_FALLBACK_LIBRARY_PATH')50def dyld_image_suffix_search(iterator, env=None):51 """For a potential path iterator, add DYLD_IMAGE_SUFFIX semantics"""52 suffix = dyld_image_suffix(env)53 if suffix is None:54 return iterator55 def _inject(iterator=iterator, suffix=suffix):56 for path in iterator:57 if path.endswith('.dylib'):58 yield path[:-len('.dylib')] + suffix + '.dylib'59 else:60 yield path + suffix61 yield path62 return _inject()63def dyld_override_search(name, env=None):64 # If DYLD_FRAMEWORK_PATH is set and this dylib_name is a65 # framework name, use the first file that exists in the framework66 # path if any. If there is none go on to search the DYLD_LIBRARY_PATH67 # if any.68 framework = framework_info(name)69 if framework is not None:70 for path in dyld_framework_path(env):71 yield os.path.join(path, framework['name'])72 # If DYLD_LIBRARY_PATH is set then use the first file that exists73 # in the path. If none use the original name.74 for path in dyld_library_path(env):75 yield os.path.join(path, os.path.basename(name))76def dyld_executable_path_search(name, executable_path=None):77 # If we haven't done any searching and found a library and the78 # dylib_name starts with "@executable_path/" then construct the79 # library name.80 if not executable_path:81 import sys82 if sys.prefix:83 executable_path = os.path.join(sys.prefix, 'bin')84 if name.startswith('@executable_path/') and executable_path is not None:85 yield os.path.join(executable_path, name[len('@executable_path/'):])86def dyld_default_search(name, env=None):87 yield name88 framework = framework_info(name)89 if framework is not None:90 fallback_framework_path = dyld_fallback_framework_path(env)91 for path in fallback_framework_path:92 yield os.path.join(path, framework['name'])93 fallback_library_path = dyld_fallback_library_path(env)94 for path in fallback_library_path:95 yield os.path.join(path, os.path.basename(name))96 if framework is not None and not fallback_framework_path:97 for path in DEFAULT_FRAMEWORK_FALLBACK:98 yield os.path.join(path, framework['name'])99 if not fallback_library_path:100 for path in DEFAULT_LIBRARY_FALLBACK:101 yield os.path.join(path, os.path.basename(name))102def dyld_find(name, executable_path=None, env=None):103 """104 Find a library or framework using dyld semantics105 """106 name = ensure_utf8(name)107 executable_path = ensure_utf8(executable_path)108 for path in dyld_image_suffix_search(chain(109 dyld_override_search(name, env),110 dyld_executable_path_search(name, executable_path),111 dyld_default_search(name, env),112 ), env):113 if os.path.isfile(path):114 return path115 raise ValueError("dylib %s could not be found" % (name,))116def framework_find(fn, executable_path=None, env=None):117 """118 Find a framework using dyld semantics in a very loose manner.119 Will take input such as:120 Python121 Python.framework122 Python.framework/Versions/Current123 """124 try:125 return dyld_find(fn, executable_path=executable_path, env=env)126 except ValueError, e:127 pass128 fmwk_index = fn.rfind('.framework')129 if fmwk_index == -1:130 fmwk_index = len(fn)131 fn += '.framework'132 fn = os.path.join(fn, os.path.basename(fn[:fmwk_index]))133 try:134 return dyld_find(fn, executable_path=executable_path, env=env)135 except ValueError:136 raise e137def test_dyld_find():138 env = {}139 assert dyld_find('libSystem.dylib') == '/usr/lib/libSystem.dylib'140 assert dyld_find('System.framework/System') == '/System/Library/Frameworks/System.framework/System'141if __name__ == '__main__':...
test_scrape_mars.py
Source:test_scrape_mars.py
1# Import Dependencies2import pandas as pd3from bs4 import BeautifulSoup as bs4import requests5from splinter import Browser6import time7# MARS NEWS URL FOR TITLE AND PARAGRAPH8url = 'http://mars.nasa.gov/news'9response = requests.get(url)10soup = bs(response.text, 'html.parser')11title = soup.find_all('div', class_='content_title')[0].text12paragraph = soup.find_all('div', class_='rollover_description_inner')[0].text13# Print statements checked during the scrape build to make sure that data was being pulled.14#print(title)15#print(paragraph)16# FEATURED IMAGE URL17executable_path = {'executable_path': 'chromedriver.exe'}18browser = Browser('chrome', **executable_path, headless=False)19image_url = 'https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars'20browser.visit(image_url)21# HTML Object22featured_html = browser.html23featured_soup = bs(featured_html, 'html.parser')24images = featured_soup.find('footer')25link = images.find('a')['data-fancybox-href']26featured_image_url = image_url + link27# Print statement checked during the scrape build to make sure that data was being pulled.28# print(featured_image_url)29# HEMISPHERE TITLES30executable_path = {'executable_path': 'chromedriver.exe'}31browser = Browser('chrome', **executable_path, headless=False)32titles = 'https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars'33browser.visit(titles)34titles_html = browser.html35titles_soup = bs(titles_html, 'html.parser')36mars_hemi_titles = titles_soup.find_all('h3')37for x in mars_hemi_titles:38 print(x.text)39# HEMISPHERE IMAGE LINKS40executable_path = {'executable_path': 'chromedriver.exe'}41browser = Browser('chrome', **executable_path, headless=False)42cerberus_link = 'https://astrogeology.usgs.gov/search/map/Mars/Viking/cerberus_enhanced'43browser.visit(cerberus_link)44cerberus_html = browser.html45cerberus_soup = bs(cerberus_html, 'html.parser')46cerberus = cerberus_soup.find('li').a['href']47#print(cerberus)48executable_path = {'executable_path': 'chromedriver.exe'}49browser = Browser('chrome', **executable_path, headless=False)50schiaparelli_link = 'https://astrogeology.usgs.gov/search/map/Mars/Viking/schiaparelli_enhanced'51browser.visit(schiaparelli_link)52schiaparelli_html = browser.html53schiaparelli_soup = bs(schiaparelli_html, 'html.parser')54schiaparelli = schiaparelli_soup.find('li').a['href']55#print(schiaparelli)56executable_path = {'executable_path': 'chromedriver.exe'}57browser = Browser('chrome', **executable_path, headless=False)58syrtis_major_link = 'https://astrogeology.usgs.gov/search/map/Mars/Viking/syrtis_major_enhanced'59browser.visit(syrtis_major_link)60syrtis_major_html = browser.html61syrtis_major_soup = bs(syrtis_major_html, 'html.parser')62syrtis_major = syrtis_major_soup.find('li').a['href']63#print(syrtis_major)64executable_path = {'executable_path': 'chromedriver.exe'}65browser = Browser('chrome', **executable_path, headless=False)66valles_marineris_link = 'https://astrogeology.usgs.gov/search/map/Mars/Viking/valles_marineris_enhanced'67browser.visit(valles_marineris_link)68valles_marineris_html = browser.html69valles_marineris_soup = bs(valles_marineris_html, 'html.parser')70valles_marineris = valles_marineris_soup.find('li').a['href']71#print(valles_marineris)72 73# MARS FACTS74executable_path = {'executable_path': 'chromedriver.exe'}75browser = Browser('chrome', **executable_path, headless=False)76mars_facts_url = 'https://space-facts.com/mars/'77mars_facts = pd.read_html(mars_facts_url)78mars_df = mars_facts[2]79mars_df.columns = ["Mars", "Mars Facts"]80mars_facts_html = mars_df.to_html()81mars_facts_html.replace('\n', '')82# Create a dictionary for the Mars title, paragraph, and URL links83mars_scraped_data = {84 "title": title,85 "paragraph": paragraph,86 "featured_image": featured_image_url,87 "cerberus": cerberus,88 "schiaparelli": schiaparelli,89 "syrtis_major": syrtis_major,90 "valles_marineris": valles_marineris,91 "facts": mars_facts_html92 }...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!