Rev 133 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
35 | t00mlabs | 1 | #!/usr/bin/python |
2 | # -*- coding: utf-8 -*- |
||
3 | # File: driver.py |
||
4 | # Author: Tomás Vírseda |
||
5 | # License: GPL v3 |
||
6 | # Description: Selenium Driver service |
||
7 | |||
8 | import os |
||
9 | import selenium |
||
10 | from selenium import webdriver |
||
11 | from selenium.webdriver.support.wait import WebDriverWait |
||
12 | from selenium import webdriver |
||
13 | from selenium.webdriver.common.keys import Keys |
||
14 | from selenium.common.exceptions import WebDriverException |
||
15 | from selenium.common.exceptions import NoSuchElementException |
||
16 | from selenium.webdriver.firefox.firefox_binary import FirefoxBinary |
||
17 | from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
||
143 | t00mlabs | 18 | from selenium.webdriver.firefox.options import Options |
35 | t00mlabs | 19 | |
143 | t00mlabs | 20 | |
35 | t00mlabs | 21 | from .service import Service |
22 | |||
23 | GECKODRIVER_URL = "https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-linux64.tar.gz" |
||
24 | |||
25 | class SeleniumDriver(Service): |
||
26 | def initialize(self): |
||
27 | self.driver = None |
||
28 | |||
29 | |||
30 | def open(self): |
||
31 | ''' |
||
32 | In order to have selenium working with Firefox and be able to |
||
33 | get SAP Notes from launchpad.support.sap.com you must: |
||
73 | t00mlabs | 34 | 1. Use a browser certificate (SAP Passport) in order to avoid |
35 | renewed logons. |
||
35 | t00mlabs | 36 | You can apply for it at: |
37 | https://support.sap.com/support-programs-services/about/getting-started/passport.html |
||
38 | 2. Get certificate and import it into Firefox. |
||
39 | Open menu -> Preferences -> Advanced -> View Certificates |
||
40 | -> Your Certificates -> Import |
||
73 | t00mlabs | 41 | 3. Trust this certificate (auto select) |
42 | 4. Check it. Visit some SAP Note url in Launchpad. |
||
43 | No credentials will be asked. |
||
44 | Launchpad must load target page successfully. |
||
35 | t00mlabs | 45 | ''' |
46 | |||
47 | if self.driver is None: |
||
48 | utils = self.get_service('Utils') |
||
143 | t00mlabs | 49 | options = Options() |
50 | options.add_argument('--headless') |
||
51 | #~ driver = webdriver.Firefox() |
||
35 | t00mlabs | 52 | FIREFOX_PROFILE_DIR = utils.get_firefox_profile_dir() |
53 | FIREFOX_PROFILE = webdriver.FirefoxProfile(FIREFOX_PROFILE_DIR) |
||
143 | t00mlabs | 54 | driver = webdriver.Firefox(firefox_profile=FIREFOX_PROFILE, firefox_options=options) |
35 | t00mlabs | 55 | self.driver = driver |
133 | t00mlabs | 56 | self.debug("Webdriver initialited") |
35 | t00mlabs | 57 | |
58 | return self.driver |
||
59 | |||
60 | |||
61 | def close(self): |
||
62 | try: |
||
63 | self.driver.quit() |
||
133 | t00mlabs | 64 | self.debug("\tWebdriver closed") |
35 | t00mlabs | 65 | except: |
133 | t00mlabs | 66 | self.debug("Webdriver already closed") |
35 | t00mlabs | 67 | pass |
68 | |||
69 | self.driver = None |
||
70 | |||
71 | |||
72 | |||
73 | def load(self, URL): |
||
74 | driver = self.open() |
||
75 | driver.get(URL) |
||
76 | return driver |
||
77 | |||
78 | |||
73 | t00mlabs | 79 | def check(self): |
80 | """ |
||
81 | Check gecko webdriver |
||
82 | You must install geckodriver. It is mandatory |
||
83 | Yo can download it from: |
||
84 | https://github.com/mozilla/geckodriver/ |
||
85 | Then, extract the binary and copy it to somewhere in your $PATH. |
||
86 | If OS is Linux: /usr/local/bin/geckodriver |
||
87 | If OS is Windows: C:\Windows\System32 or elsewhere. |
||
88 | |||
89 | Basico will try to do it for you. |
||
90 | """ |
||
91 | utils = self.get_service('Utils') |
||
92 | |||
93 | # First, add BASICO OPT Path to $PATH |
||
94 | GECKO_INSTALL_DIR = self.get_var('DRIVERS', 'local') |
||
95 | os.environ["PATH"] += os.pathsep + GECKO_INSTALL_DIR |
||
96 | # Then, look for Geckodriver |
||
97 | GECKODRIVER = utils.which('geckodriver') |
||
98 | |||
99 | if not GECKODRIVER: |
||
133 | t00mlabs | 100 | self.debug("Attempting to download Gecko driver and install it.") |
73 | t00mlabs | 101 | utils.install_geckodriver() |
102 | |||
103 | GECKODRIVER = utils.which('geckodriver') |
||
104 | if GECKODRIVER is None: |
||
105 | self.log.warning("Gecko driver not found.") |
||
106 | return False |
||
107 | else: |
||
133 | t00mlabs | 108 | self.debug("Gecko Webdriver found in: %s" % GECKODRIVER) |
73 | t00mlabs | 109 | return True |
110 | |||
111 | |||
35 | t00mlabs | 112 | def run(self): |
113 | pass |
||
114 | |||
115 | |||
116 | def quit(self): |
||
117 | self.close() |
||
118 | |||
119 | |||
120 | def end(self): |
||
121 | self.close() |
||
122 | |||
123 |