def request_response(self, formspec=None): self.start_server() if formspec: formdata = open(formspec, "rb") else: formdata = json.dumps(self.custom_form) headers = { "Accept": "application/json", "Content-Type": "application/json" } requests.post( f"{self.attended_server}/formspec", data=formdata, headers=headers, ) br = Browser() br.open_browser(f"{self.attended_server}/form.html") br.set_window_size(600, 800) headers = {"Prefer": "wait=120"} response_json = None # etag = None while True: # if etag: # headers['If-None-Match'] = etag headers["If-None-Match"] = "2434432243" response = requests.get(f"{self.attended_server}/requestresponse", headers=headers) # etag = response.headers.get("ETag") if response.status_code == 200: try: response_json = response.json() break except JSONDecodeError: pass elif response.status_code != 304: # back off if the server is throwing errors time.sleep(60) continue time.sleep(1) br.close_browser() return response_json
def request_response(self, formspec: str = None, window_width: int = 600, window_height: int = 1000) -> dict: """Start server and show form. Waits for user response. :param formspec: form json specification file, defaults to None :param window_width: window width in pixels, defaults to 600 :param window_height: window height in pixels, defaults to 1000 :return: form response Example. .. code-block:: robotframework Create Form ${CURDIR}/${/}myform.json &{response} Request Response """ self._start_attended_server() if self.custom_form is None: self.create_form("Requesting response") if formspec: formdata = open(formspec, "rb") else: formdata = json.dumps(self.custom_form) headers = { "Accept": "application/json", "Content-Type": "application/json" } requests.post( f"{self.server_address}/formspec", data=formdata, headers=headers, ) response_json = {} try: br = Browser() br.open_available_browser(f"{self.server_address}/form.html") br.set_window_size(window_width, window_height) headers = {"Prefer": "wait=120"} # etag = None while True: # if etag: # headers['If-None-Match'] = etag headers["If-None-Match"] = "2434432243" response = requests.get( f"{self.server_address}/requestresponse", headers=headers) # etag = response.headers.get("ETag") if response.status_code == 200: try: response_json = response.json() break except JSONDecodeError: break elif response.status_code != 304: # back off if the server is throwing errors time.sleep(10) continue time.sleep(1) finally: br.close_browser() self._stop_attended_server() return response_json
def test_import(self): from RPA.Browser import Browser Browser()
from RPA.Browser import Browser browser = Browser() url = "https://robocorp.com/docs/" term = "python" screenshot_filename = "output/screenshot.png" def open_the_website(url: str): browser.open_available_browser(url) def search_for(term: str): input_field = "css:input" browser.input_text(input_field, term) browser.press_keys(input_field, "ENTER") def store_screenshot(filename: str): browser.screenshot(filename=filename) # Define a main() function that calls the other functions in order: def main(): try: open_the_website(url) search_for(term) store_screenshot(screenshot_filename) finally: browser.close_all_browsers()
import subprocess from RPA.Browser import Browser from webdrivermanager import ChromeDriverManager logging.basicConfig( level=logging.DEBUG, format="[{%(name)s:%(filename)s:%(lineno)d} %(levelname)s - %(message)s", handlers=[logging.FileHandler(filename="debug.log", mode="w")], ) logger = logging.getLogger(__name__) cdm = ChromeDriverManager() def runcmd(command, loglabel): info = subprocess.run(command, shell=True, capture_output=True, universal_newlines=True) logger.info(f"{loglabel} {info.stdout.strip()}") runcmd("python --version", "PYTHON VERSION:") runcmd("pip list", "PIP LIST:\n\n") logger.info(f"ChromeDriverManager link path: {cdm.link_path}") logger.info(f"ChromeDriverManager download root: {cdm.download_root}") br = Browser() br.open_available_browser("https://www.google.com") br.close_all_browsers()
def test_imports(self): from RPA.Browser import Browser from RPA.Browser.Selenium import Selenium Browser() Selenium()
from RPA.Browser import Browser from RPA.FileSystem import FileSystem browser = Browser() # 需要进行类的实例化。 file_system = FileSystem() # 进行类的示例化。 url = "http://news.baidu.com" def store_web_page_content(): browser.open_available_browser(url) text = browser.get_text("body") file_system.create_file("output/text.txt", text, overwrite=True) browser.screenshot() def main(): try: store_web_page_content() finally: browser.close_all_browsers() if __name__ == '__main__': main()
def run_with_rpabrowser(rh, sheet): print("Running RPA Challenge with RPA Browser (Selenium)") browser = Browser() browser.open_available_browser("http://www.rpachallenge.com", headless=True) browser.click_button_when_visible("//button[text()='Start']") for row in sheet: myeval(browser, '//input[@ng-reflect-name="labelFirstName"]', row["First Name"]) myeval(browser, '//input[@ng-reflect-name="labelLastName"]', row["Last Name"]) myeval( browser, '//input[@ng-reflect-name="labelCompanyName"]', row["Company Name"], ) myeval(browser, '//input[@ng-reflect-name="labelRole"]', row["Role in Company"]) myeval(browser, '//input[@ng-reflect-name="labelAddress"]', row["Address"]) myeval(browser, '//input[@ng-reflect-name="labelEmail"]', row["Email"]) myeval( browser, '//input[@ng-reflect-name="labelPhone"]', str(row["Phone Number"]) ) browser.execute_javascript( """document.querySelector('input[type="submit"]').click();""" ) result = browser.execute_javascript( """return document.querySelector('.message2').textContent;""" ) rh.check_for_new_record(browser, result) browser.close_browser()