def main(position, location): """Run the main program routine""" scraped_jobs = [] scraped_urls = set() url = get_url(position, location) # setup web driver options = EdgeOptions() options.use_chromium = True driver = Edge(options=options) driver.implicitly_wait(5) driver.get(url) # extract the job data while True: cards = driver.find_elements_by_class_name('jobsearch-SerpJobCard') get_page_records(cards, scraped_jobs, scraped_urls) try: driver.find_element_by_xpath('//a[@aria-label="Next"]').click() except NoSuchElementException: break except ElementNotInteractableException: driver.find_element_by_id('popover-x').click() # to handle job notification popup get_page_records(cards, scraped_jobs, scraped_urls) continue # shutdown driver and save file driver.quit() save_data_to_file(scraped_jobs)
def Restart_Modem(): options = EdgeOptions() options.use_chromium = True options.add_argument("-inprivate") edge_browser = Edge(options=options) edge_browser.get('http://192.168.0.1') time.sleep(2) print('Welcome') try: login = edge_browser.find_element_by_name('username') password = edge_browser.find_element_by_name('password') sign = edge_browser.find_element_by_class_name('styled_button_s') login.clear() password.clear() login.send_keys('admin') password.send_keys('admin') time.sleep(2) sign.click() print('Sign in') alert = Alert(edge_browser) time.sleep(2) edge_browser.get('http://192.168.0.1/saveconf.htm') time.sleep(2) system = edge_browser.find_element_by_id('three_level_menu1') system.click() time.sleep(2) reboot = edge_browser.find_element_by_name('reboot') reboot.click() alert.accept() time.sleep(70) print('Reboot') edge_browser.quit() except: print('Problem with restarting modem') edge_browser.quit()
def Webpage_login(self,scanner_url, username, password): """edge浏览器""" edgedriver = "./msedgedriver.exe" #这里写本地的edgedriver的所在路径 edge_options = EdgeOptions() edge_options.use_chromium = True # 使用谷歌内核 edge_options.add_argument("disable-gpu") # 禁用gpu加速,避免bug edge_options.add_argument('start-maximized') # 启动最大化 edge_options.add_argument('--ignore-certificate-errors') # 绕过“你的连接不是专用连接” edge_options.add_experimental_option('useAutomationExtension', False) # 关闭“Microsoft Edge正由自动测试软件控制。” edge_options.add_experimental_option('excludeSwitches', ['enable-automation']) driver = Edge(executable_path=edgedriver,options=edge_options) driver.get(scanner_url) time.sleep(1) driver.find_element_by_id('username').click() driver.find_element_by_id('username').send_keys(username) driver.find_element_by_id('password').click() driver.find_element_by_id('password').send_keys(password) driver.find_element_by_class_name('submit').click() time.sleep(1) driver.find_element_by_id('two01').click()
class Aplicacion(): def __init__(self): self.ventana1=tk.Tk() self.ventana1.title('TFS') self.ventana1.geometry('270x250') #self.ventana1.iconbitmap('C:\\Users\\JorgeDanielGarcia\\Desktop\\Descarga.ico\\') self.label1=tk.Label(self.ventana1,text="Ticket:") self.label1.grid(column=0, row=0,) self.dato1=tk.IntVar() self.entry1=tk.Entry(self.ventana1, width=30, textvariable=self.dato1) self.entry1.grid(column=1, row=0) self.entry1.insert(0, "123456") self.label2=tk.Label(self.ventana1,text="Carpeta:") self.label2.grid(column=0, row=1) self.dato2=tk.StringVar() self.entry2=tk.Entry(self.ventana1, width=30, textvariable=self.dato2) self.entry2.grid(column=1, row=1) radioGroup = LabelFrame(self.ventana1, text = "Seleccionar modo de ejecucion") radioGroup.grid(column=0, row=4 , columnspan = 5 , pady = 2) self.seleccion=tk.IntVar() self.radio1=tk.Radiobutton(radioGroup,text="Background", variable=self.seleccion, value=1) self.radio1.grid(column=0, row=2) self.radio2=tk.Radiobutton(radioGroup,text="Online", variable=self.seleccion, value=2) self.radio2.grid(column=1, row=2) self.boton1=tk.Button(self.ventana1, text="Upload", command=self.upload) self.boton1.grid(column=1, row=10) # self.boton1.config(bg='white') self.boton2=tk.Button(self.ventana1, text=" /\ ",command=self.directory) self.boton2.grid(column=5, row=1) # self.boton2.config(bg='white') # self.ventana1.config(bg='light grey') self.ventana1.mainloop() def directory(self): self.root = tk.Tk() self.root.withdraw() self.folder_selected = filedialog.askdirectory() buscar = "/" reemplazar = "\\" carpeta = self.folder_selected.replace(buscar,reemplazar) carpeta2 = carpeta + reemplazar self.entry2.insert(0, carpeta2) print(carpeta2) def validacion1(self): ticket= self.dato1.get() # ticket dire = self.dato2.get() # dire print (ticket) print (dire) # cantdocs=len(glob.glob(dire + "*")) # validacion cant de documentos, si no hay documentos en la carpeta, error try: os.listdir(dire) error = 0 cantdocs=len(glob.glob(dire + "*")) except Exception as e: print(e) error = 1 if error == 0: if cantdocs == 0: self.label4=tk.Label(self.ventana1,text= "No hay docs en la carpeta") self.label4.grid(column=1, row=15) error = 1 return error def accesotfs(self, ruta, dire): # self.robot = robot ticket1 = str(self.dato1.get()) #self.chrome_options = Options() #if self.seleccion.get()==1: #self.chrome_options.add_argument("--headless") # self.driver = webdriver.Chrome(executable_path=r"C:\Google\chromedriver") # self.driver = webdriver.Edge(executable_path=r"C:\Google\msedgedriver") self.edge_option = EdgeOptions() self.edge_option.add_argument("hide_console") # options.add_argument = ["hide_console"] # self.driver = webdriver.Edge(options) # self.driver = webdriver.Edge("C:\Google\msedgedriver", options=self.edge_option) self.driver = Edge("C:\Google\msedgedriver", service_args= ["hide_console"]) url = "http://10.1.27.11:8080/tfs/TFSYPF/E2E/_workitems?_a=edit&id=" urlarmada = url + ticket1 # Conectarse self.driver.get(urlarmada) self.driver.implicitly_wait(8) # self.robot.typewrite("SE33439") # time.sleep(1) # self.robot.press('tab') # time.sleep(1) # robot.typewrite("Homeroibm2020-") # time.sleep(1) # self.robot.press('tab') # time.sleep(1) # self.robot.press('enter') # time.sleep(4) # dirigirse hacia el modulo attachment self.attachment = "ui-id-7" self.driver.find_element_by_id(self.attachment).click() # boton agregar adjuntos # time.sleep(1) self.driver.implicitly_wait(5) for i in ruta: nombre_archivo = i direarchivo= dire + nombre_archivo self.driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/div[2]/div/div[3]/div[4]/div[2]/div/div[2]/div[2]/table/tbody/tr[5]/td/table/tbody/tr/td[2]/table/tbody/tr/td/div/div[3]/table/tbody/tr/td/div/ul/li[2]").click() # time.sleep(1) # boton seleccionar archivo self.driver.implicitly_wait(5) self.driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/form/input[1]").send_keys(direarchivo) # time.sleep(1) # boton aceptar self.driver.implicitly_wait(5) self.driver.find_element_by_xpath("/html/body/div[4]/div[3]/div/button[1]").click() # time.sleep(1) # boton guardar self.driver.implicitly_wait(5) self.driver.implicitly_wait(5) self.driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/div[2]/div/div[3]/div[4]/div[2]/div/div[2]/div[1]/ul/li[2]").click() def upload(self): if self.seleccion.get()==1 or self.seleccion.get()==2: resul = self.validacion1() if resul == 0: print ("todo ok") # self.label3=tk.Label(self.ventana1,text= cantdocs) # self.label3.grid(column=0, row=6) # self.label4=tk.Label(self.ventana1,text= "docs en Direccion indicada") # self.label4.grid(column=1, row=6) # self.label5=tk.Label(self.ventana1,text= cantdocs) # self.label5.grid(column=0, row=7) # self.label6=tk.Label(self.ventana1,text= "docs subidos a TFS") # self.label6.grid(column=1, row=7) # self.popup() dire = self.dato2.get() # dire # validacion si algun archivo pesa mas de 4mb - #4.194.304 si es mayor a este numero, entonces pesa mas de 4mb el archivo ruta = os.listdir(dire) print(ruta) for i in ruta: nombre_archivo = i direarchivo= dire + nombre_archivo sizefile = os.stat(direarchivo).st_size print(direarchivo, "--- este archivo pesa", sizefile , "bytes") # validacion existencia de usuario y contrasena #keyQ = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Environment', 0, winreg.KEY_QUERY_VALUE) #try: # usuario = winreg.QueryValueEx(keyQ, "UsuarioE2E") # contrasena = winreg.QueryValueEx(keyQ, "PassE2E") # error = 0 #except Exception as e: # print(e) # error = 1 #if error == 1: # print("No existe usuario o contrasena") #if error == 0: # print("todo bien") # print(usuario[0]) # print(contrasena[0]) #armado de ruta para acceder a TFS #if ruta: conectar = self.accesotfs(ruta, dire)
class QCourse: def __init__(self): # 初始化options self.prefs = {"download.default_directory": os.getcwd()} self.options = EdgeOptions() self.options.use_chromium = True self.options.add_argument("log-level=3") self.options.add_experimental_option('excludeSwitches', ['enable-logging']) self.options.add_experimental_option('prefs', self.prefs) self.options.add_argument("--mute-audio") self.login_url = 'https://ke.qq.com/' # Mac 下配置 options 报错,故扔掉了。如果是 Windows,请使用路径下面的 msedgedriver.exe。(注释掉下面一行,放开下下行) self.driver = Edge(executable_path=os.path.join( BASE_DIR, 'msedgedriver'), capabilities={}) # self.driver = Edge(executable_path='msedgedriver.exe', options=self.options) # self.driver = Edge(executable_path=os.path.join(BASE_DIR, 'msedgedriver'), capabilities=desired_cap, options=self.options) def login(self): self.driver.get('https://ke.qq.com/') self.driver.find_element_by_id('js_login').click() time.sleep(1) WebDriverWait(self.driver, 300).until_not( EC.presence_of_element_located((By.CLASS_NAME, 'ptlogin-mask'))) dictCookies = self.driver.get_cookies() jsonCookies = json.dumps(dictCookies) with open('cookies.json', 'w') as f: f.write(jsonCookies) print('登陆成功!') def close(self): self.driver.close() def _get_video(self, video_url=None, path=None, index=None): if not video_url: print('请输入视频url!') # 跳转一次没法跳转,可能是设置了preventDefault self.driver.get(video_url) self.driver.get(video_url) try: # 等待视频开始播放 WebDriverWait(self.driver, 60).until( EC.presence_of_element_located((By.CLASS_NAME, 'loki-time'))) WebDriverWait( self.driver, 60).until_not(lambda driver: driver.find_element_by_class_name( 'loki-time').get_attribute("innerHTML") == '00:00 / 00:00') title = self.driver.title if index is not None: title = "{:02}_{}".format(index, title) networks = self.driver.execute_script( 'return window.performance.getEntries()') ts_url = key_url = '' for network in networks: if '.ts?start' in network.get('name'): ts_url = network.get('name') elif 'get_dk' in network.get('name'): key_url = network.get('name') download_single(ts_url, key_url, title, path) except TimeoutException: # 如果超时,可能是下载的资料,则查看是否有下载按钮,有的话,就下载 title = self.driver.title try: down_btn = self.driver.find_element_by_class_name( 'download-btn') if down_btn.text == '下载资料': url = down_btn.get_attribute('href') download_zip_doc(url, title, path) except Exception: print('没有找到视频,也没有找到可下载的文件,可能是还未开课') def get_video(self, video_url=None, path=None, index=None): if isinstance(video_url, list): for url in video_url: if url: self._get_video(url, path, index) else: self._get_video(video_url, path, index) def load_cookies(self): if not os.path.exists('cookies.json'): self.login() with open('cookies.json', 'r') as f: listCookies = json.loads(f.read()) self.driver.get(self.login_url) for cookie in listCookies: self.driver.add_cookie({ 'domain': '.ke.qq.com', 'httpOnly': cookie['httpOnly'], 'name': cookie['name'], 'path': '/', 'secure': cookie['secure'], 'value': cookie['value'] }) for cookie in utils.get_cookies_dic_list(): self.driver.add_cookie({ 'domain': '.ke.qq.com', 'httpOnly': False, 'name': cookie[0], 'path': '/', 'secure': False, 'value': cookie[1] })
class Chat: def __init__(self): self.login = None self.password = None self.message = None self.thread = None self.picture = None self._driver = None self._session = None self._executor_url = None self._base_url = 'https://www.messenger.com/' self._initiate() def _initiate(self): try: with open('SessionExecutor.txt') as f: data = f.readlines() data = [a.strip() for a in data] self._session = data[0] self._executor_url = data[1] self._driver = webdriver.Remote(command_executor=self._executor_url, desired_capabilities={}, options=options) self._driver.session_id = self._session print('same browser') except (FileNotFoundError, IndexError, MaxRetryError): self._driver = Edge("./msedgedriver.exe", options=options) with open('SessionExecutor.txt', 'w+', encoding='utf-8') as f: f.write(self._driver.session_id) f.write('\n') f.write(self._driver.command_executor._url) print("new browser") def _log_in(self): self._driver.get(self._base_url) self._driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div/div/div/div[3]/button[2]').click() self._driver.find_element_by_id("email").send_keys(self.login) self._driver.find_element_by_id("pass").send_keys(self.password, Keys.RETURN) def send_message(self, thread, message, login, password): try: self.message = message self.login = login self.password = password self.thread = thread except ValueError: return 'Lacking key parameters' if "/t" not in self._driver.current_url: self._log_in() url = self._base_url + 't/' + self.thread if self._driver.current_url != url: self._driver.get(url) WebDriverWait(self._driver, 10).until(ec.presence_of_element_located((By.CLASS_NAME, "_5rp7"))) self._driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[2]/div/div/div[1]/div[1]/ \ div[2]/div/div/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/form/div/div[3]/div[2]/div[1]/div/ \ div/div/div/div[2]/div/div/div/div').send_keys(self.message, Keys.RETURN) def send_picture(self, thread, picture, login, password): try: self.picture = picture self.login = login self.password = password self.thread = thread except ValueError: return 'Lacking key parameters' if "/t" not in self._driver.current_url: self._log_in() url = self._base_url + 't/' + self.thread if self._driver.current_url != url: self._driver.get(url) WebDriverWait(self._driver, 10).until(ec.presence_of_element_located((By.XPATH, "/html/body/div[1] \ /div/div[1]/div/div[2]/div/div/div[1]/div[1]/div[2]/div/div/div/div/div/div/div[2]/div/div/div/ \ div[2]/div/form/div/div[3]/div[1]/input"))) upload_picture = self._driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[2]/ \ div/div/div[1]/div[1]/div[2]/div/div/div/div/div/div/div[2]/div/div/div/div[2]/div/form/div/ \ div[3]/div[1]/input') upload_picture.send_keys(self.picture) time.sleep(2) self._driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[2]/div/div/div[1]/ \ div[1]/div[2]/div/div/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/form/div/div[3]/div[2] \ /div[1]/div/div/div/div/div[2]/div/div/div/div').send_keys(Keys.RETURN) def exit(self): self._driver.quit() os.system('cmd /c "taskkill /IM msedgedriver.exe /F /T"')
MSEDGEDRIVER_PATH= "C:\\edgedriver_win64\\msedgedriver.exe" if __name__ == "__main__": service = EdgeService(MSEDGEDRIVER_PATH) options = EdgeOptions() options.use_chromium = True driver = Edge(executable_path=MSEDGEDRIVER_PATH,options=options) # Synthetic test BASE_URL = "http://www.yourapp.com" driver.get(BASE_URL+"/ims.html") time.sleep(3) try: element = driver.find_element_by_id("Home") element.click() time.sleep(3) element = driver.find_element_by_id("Orders") element.click() time.sleep(3) element = driver.find_element_by_id("Portfolio") element.click() time.sleep(3) element = driver.find_element_by_id("Securities") element.click() time.sleep(3)
class QCourse: def __init__(self): # 初始化options self.prefs = {"download.default_directory": os.getcwd()} self.options = EdgeOptions() self.options.use_chromium = True self.options.add_argument("log-level=3") self.options.add_experimental_option('excludeSwitches', ['enable-logging']) self.options.add_experimental_option('prefs', self.prefs) self.options.add_argument("--mute-audio") self.login_url = 'https://ke.qq.com/' self.driver = Edge(executable_path='msedgedriver.exe', options=self.options) def login(self): self.driver.get('https://ke.qq.com/') self.driver.find_element_by_id('js_login').click() time.sleep(1) WebDriverWait(self.driver, 300).until_not( EC.presence_of_element_located((By.CLASS_NAME, 'ptlogin-mask'))) dictCookies = self.driver.get_cookies() jsonCookies = json.dumps(dictCookies) with open('cookies.json', 'w') as f: f.write(jsonCookies) print('登陆成功!') def close(self): self.driver.close() def get_video(self, video_url=None, path=None): if not video_url: print('请输入视频url!') # os.chdir(BASE_DIR) if not os.path.exists('cookies.json'): self.login() with open('cookies.json', 'r') as f: listCookies = json.loads(f.read()) self.driver.get(video_url) for cookie in listCookies: self.driver.add_cookie({ 'domain': '.ke.qq.com', 'httpOnly': cookie['httpOnly'], 'name': cookie['name'], 'path': '/', 'secure': cookie['secure'], 'value': cookie['value'] }) self.driver.get(video_url) # 等待视频开始播放 WebDriverWait(self.driver, 300).until( EC.presence_of_element_located((By.CLASS_NAME, 'loki-time'))) WebDriverWait( self.driver, 300).until_not(lambda driver: driver.find_element_by_class_name( 'loki-time').get_attribute("innerHTML") == '00:00 / 00:00') networks = self.driver.execute_script( 'return window.performance.getEntries()') ts_url = key_url = '' for network in networks: if '.ts?start' in network.get('name'): ts_url = network.get('name') elif 'get_dk' in network.get('name'): key_url = network.get('name') title = self.driver.title # catalog = self.driver.execute_script('return document.getElementsByClassName("task-item task-info active")' # '[0].parentNode.firstElementChild.innerText') # os.chdir(os.path.join(os.getcwd(), catalog)) download_single(ts_url, key_url, title, path)
def download(url): options = EdgeOptions() options.use_chromium =True # option = webdriver.ChromeOptions() # option.add_argument('headless') options.add_argument('log-level=3') driver = Edge(options=options) # driver = webdriver.Chrome( # executable_path='.//chromedriver', chrome_options=option) title = "output" try: driver.set_page_load_timeout(15) driver.get(url) title = driver.title except: print("Timeout - start download anyway.") print(f'道客巴巴: 《{title}》') time.sleep(5) try: # 展开全部 elem_cont_button = driver.find_element_by_id("continueButton") driver.execute_script( "arguments[0].scrollIntoView(true);", elem_cont_button) actions = ActionChains(driver) actions.move_to_element(elem_cont_button).perform() time.sleep(0.5) elem_cont_button.click() except NoSuchElementException: pass # 获取页数 num_of_pages = driver.find_element_by_id('readshop').find_element_by_class_name( 'mainpart').find_element_by_class_name('shop3').find_element_by_class_name('text').get_attribute('innerHTML') num_of_pages = int(num_of_pages.split(' ')[-1]) for i in range(5): # 缩放 driver.find_element_by_id('zoomInButton').click() time.sleep(0.5) if os.path.exists(f'./temp/{title}'): shutil.rmtree(f'./temp/{title}') os.makedirs(f'./temp/{title}') for pages in trange(num_of_pages): time.sleep(0.5) canvas_id = "page_" + str(pages + 1) pagepb_id = "pagepb_" + str(pages + 1) element = driver.find_element_by_id(canvas_id) driver.execute_script("arguments[0].scrollIntoView(true);", element) actions = ActionChains(driver) actions.move_to_element(element).perform() time.sleep(0.5) # Check loading status while(len(driver.find_element_by_id(pagepb_id).get_attribute('innerHTML')) != 0): time.sleep(1) # print(driver.find_element_by_id( # pagepb_id).get_attribute('innerHTML')) js_cmd = "var canvas = document.getElementById('{}');".format(canvas_id) + \ "return canvas.toDataURL();" img_data = driver.execute_script(js_cmd) img_data = (img_data[22:]).encode() with open(f"./temp/{title}/{pages}.png", "wb") as fh: fh.write(base64.decodebytes(img_data)) driver.quit() print('下载完毕,正在转码') conpdf(f'output/{title}.pdf', f'temp/{title}', '.png')
from msedge.selenium_tools import Edge, EdgeOptions from selenium.webdriver.common.keys import Keys import time import random options = EdgeOptions() options.use_chromium = True word_list=["stack overflow how to capture traffic", "java display thread", "p2p network"] driver = Edge(options=options) driver.get("https://www.google.com/") driver.maximize_window() time.sleep(1) inputElement = driver.find_element_by_id("zV9nZe") inputElement.click() inputElement = driver.find_element_by_xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input") inputElement.send_keys(random.choice(word_list), Keys.ENTER) element = driver.find_element_by_xpath("/html/body/div[7]/div/div[9]/div[1]/div/div[2]/div[2]/div/div/div/div[1]/div/div[1]/a") time.sleep(1) driver.get(element.get_attribute('href')) time.sleep(1) lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;") time.sleep(4) driver.close()
from msedge.selenium_tools import options from msedge.selenium_tools import Edge, EdgeOptions from time import sleep options = EdgeOptions() options.use_chromium = True driver = Edge(options=options) usr = "******" pwd = "D9806f2a2b" driver.get("https://www.facebook.com/") print("Opened Facebook") sleep(0.5) username_box = driver.find_element_by_id("email") username_box.send_keys(usr) print("Email ID entered") sleep(0.1) password_box = driver.find_element_by_id("pass") password_box.send_keys(pwd) print("Password entered") sleep(0.5) enter_box = driver.find_element_by_id("u_0_b").click() print("Login pressed")
print("no hay numero de 7 digitos") else: print("el ticket debe ser", ticket) print(lista) ticket = lista[0] print(ticket) edge_option = EdgeOptions() edge_option.add_argument("hide_console") driver = Edge("C:\Google\msedgedriver", service_args=["hide_console"]) url = "http://10.1.27.11:8080/tfs/TFSYPF/E2E/_workitems?_a=edit&id=" urlarmada = url + ticket driver.get(urlarmada) driver.implicitly_wait(8) attachment = "ui-id-7" driver.find_element_by_id(attachment).click() direarchivo = dir_inicial + nombre_archivo driver.implicitly_wait(5) driver.find_element_by_xpath( "/html/body/div[4]/div[2]/div/form/input[1]").send_keys( direarchivo) driver.implicitly_wait(5) driver.find_element_by_xpath( "/html/body/div[4]/div[3]/div/button[1]").click() driver.implicitly_wait(5) driver.implicitly_wait(5) driver.find_element_by_xpath( "/html/body/div[2]/div/div[2]/div/div[2]/div/div[3]/div[4]/div[2]/div/div[2]/div[1]/ul/li[2]"
options.use_chromium = True driver = Edge(options=options) print("Getting atlas") # get opportunity atlas driver.get("https://opportunityatlas.org/") time.sleep(10) # click on get started atlas get_started = driver.find_element_by_xpath( "/html/body/div[3]/div[2]/div[1]/p[5]/button") get_started.click() # find element element = driver.find_element_by_id('introductionDialog') driver.execute_script( """ var element = arguments[0]; element.parentNode.removeChild(element); """, element) search_box = driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div[2]/div[3]/div[2]/input') search_box.clear() search_box.send_keys(tract_id) time.sleep(2) search_box.send_keys(Keys.ENTER) time.sleep(10) driver.save_screenshot('test.png')
class Sei: __area_inicial = None __windows_before = 0 __windows_after = 0 def __init__(self, headless=False, executable_path='chromedriver'): if 'chromedriver' in executable_path: chrome_options = Options() chrome_options.add_argument('--enable-javascript') chrome_options.add_argument('--window-size=1440,900') chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--proxy-server='direct://'") chrome_options.add_argument("--proxy-bypass-list=*") chrome_options.add_argument("--start-maximized") chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--ignore-certificate-errors') if headless: chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') self.driver = webdriver.Chrome(executable_path=executable_path, options=chrome_options) elif 'msedgedriver' in executable_path: edge_options = EdgeOptions() edge_options.use_chromium = True edge_options.add_argument('enable-javascript') edge_options.add_argument('window-size=1440,900') edge_options.add_argument("disable-extensions") edge_options.add_argument("proxy-server='direct://'") edge_options.add_argument("proxy-bypass-list=*") edge_options.add_argument("start-maximized") edge_options.add_argument('disable-dev-shm-usage') edge_options.add_argument('no-sandbox') edge_options.add_argument('ignore-certificate-errors') if headless: edge_options.add_argument('headless') edge_options.add_argument('disable-gpu') self.driver = Edge(executable_path=executable_path, options=edge_options) def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.close() def start_driver(self, url, usuario=None, senha=None): if usuario == None: usuario = input('Digite o usuário: ') if senha == None: senha = getpass('Digite a senha: ') self.driver.get(url) usuario_field = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "txtUsuario"))) senha_field = self.driver.find_element_by_id('pwdSenha') botao_acessar = self.driver.find_element_by_id('sbmLogin') usuario_field.clear() usuario_field.send_keys(usuario) senha_field.clear() senha_field.send_keys(senha) botao_acessar.click() alerta = self.fechar_alerta() if alerta: raise Exception(alerta) # usuário ou senha inválido self.__area_incial = self.get_area() def go_to(self, numero_sei): if self.__windows_after > self.__windows_before: self.driver.close() self.driver.switch_to.window( self.driver.window_handles[self.__windows_before - 1]) self.driver.switch_to.default_content() pesquisa = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "txtPesquisaRapida"))) pesquisa.clear() pesquisa.send_keys(str(numero_sei)) formPesquisaRapida = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located( (By.ID, "frmProtocoloPesquisaRapida"))) self.__windows_before = len(self.driver.window_handles) formPesquisaRapida.submit() self.__windows_after = len(self.driver.window_handles) if self.__windows_after > self.__windows_before: self.driver.switch_to.window( self.driver.window_handles[self.__windows_after - 1]) def is_processo_aberto(self, area=None, processo=None): if processo: self.go_to(processo) else: self.driver.switch_to.default_content() try: ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) informacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "divInformacao"))) mensagem = informacao.text aberto = 'aberto' in mensagem if area: regex = '(?im)^(.*)(' + area + ')[^0-9a-z](.*)$' matches = search(regex, mensagem) if matches: aberto = True else: aberto = False self.driver.switch_to.default_content() except: aberto = None mensagem = 'Impossível abrir mensagem do processo' return aberto, mensagem def get_processo_anexador(self, processo=None): if processo: self.go_to(processo) else: self.driver.switch_to.default_content() ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) informacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "divInformacao"))) procAnex = None if 'Processo anexado ao processo' in informacao.text: processoAnexador = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located( (By.XPATH, "//*[@id=\"divInformacao\"]/div/a"))) procAnex = processoAnexador.text self.driver.switch_to.default_content() return procAnex def get_area(self): self.driver.switch_to.default_content() select = Select(self.driver.find_element_by_id('selInfraUnidades')) return select.all_selected_options[0].text def seleciona_area(self, area): self.driver.switch_to.default_content() select = Select(self.driver.find_element_by_id('selInfraUnidades')) all_selected_options = select.all_selected_options for option in all_selected_options: if area == option.text: return True select = Select(self.driver.find_element_by_id('selInfraUnidades')) options = select.options for option in options: if area == option.text: select.select_by_visible_text(area) Select( WebDriverWait(self.driver, 3).until( EC.presence_of_element_located( (By.ID, 'selInfraUnidades')))) return True return False def clicar_botao(self, botao): self.driver.switch_to.default_content() ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) arvore = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "divArvoreAcoes"))) botoes = arvore.find_elements(By.XPATH, '//*[@id=\"divArvoreAcoes\"]/a') for b in botoes: img = b.find_element(By.XPATH, 'img') if botao in img.get_attribute('title'): b.click() try: WebDriverWait(self.driver, 1).until( EC.alert_is_present(), 'Timed out waiting for PA creation ' + 'confirmation popup to appear.') except: try: self.driver.switch_to.default_content() except: None return True return False def fechar_alerta(self): alerta = None try: WebDriverWait(self.driver, 3).until( EC.alert_is_present(), 'Timed out waiting for PA creation ' + 'confirmation popup to appear.') alert = self.driver.switch_to.alert alerta = alert.text alert.accept() self.driver.switch_to.default_content() except TimeoutException: None return alerta def is_sobrestado(self, area=None, processo=None): if processo: self.go_to(processo) else: self.driver.switch_to.default_content() ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) informacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "divInformacao"))) sobrestado = 'sobrestado' in informacao.text mensagem = informacao.text self.driver.switch_to.default_content() if area: regex = '(?im)^(.*)(' + area + ')[^0-9a-z](.*)$' matches = search(regex, informacao.text) return sobrestado, matches != None else: return sobrestado, mensagem def sobrestar_processo(self, motivo, processo=None): if processo: self.go_to(processo) else: self.driver.switch_to.default_content() if self.clicar_botao('Sobrestar Processo'): ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) self.driver.find_element(By.ID, 'divOptSomenteSobrestar').click() motivoField = self.driver.find_element(By.ID, 'txaMotivo') motivoField.clear() motivoField.send_keys(motivo) self.driver.find_element(By.ID, 'sbmSalvar').click() self.driver.switch_to.default_content() return True return False def remover_sobrestamento(self, processo=None): if processo: self.go_to(processo) if self.clicar_botao('Remover Sobrestamento do Processo'): self.fechar_alerta() return True return False def publicar(self, resumo_ementa, data_disponibilizacao, documento=None, dou=False, secao=None, pagina=None): if documento: self.go_to(documento) else: self.driver.switch_to.default_content() if self.clicar_botao('Agendar Publicação'): ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) resumo_ementa_text_field = self.driver.find_element( By.ID, 'txaResumo') resumo_ementa_text_field.clear() resumo_ementa_text_field.send_keys(resumo_ementa) disponibilizacao = self.driver.find_element( By.ID, 'txtDisponibilizacao') disponibilizacao.clear() disponibilizacao.send_keys(data_disponibilizacao) if dou: select = Select(self.driver.find_element_by_id('selVeiculoIO')) select.select_by_visible_text('DOU') select = Select( WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "selSecaoIO")))) WebDriverWait(self.driver, 3).until( EC.presence_of_element_located( (By.CSS_SELECTOR, "option[value='" + secao if secao else '3' + "']"))) select.select_by_visible_text(secao if secao else '3') pagina_text_field = self.driver.find_element( By.ID, 'txtPaginaIO') pagina_text_field.clear() pagina_text_field.send_keys(pagina if pagina else '') disponibilizacao = self.driver.find_element(By.ID, 'txtDataIO') disponibilizacao.clear() disponibilizacao.send_keys(data_disponibilizacao) self.driver.find_element_by_id('btnSalvar').click() self.driver.switch_to.default_content() return True return False def get_conteudo_documento(self, documento=None): if documento: self.go_to(documento) else: self.driver.switch_to.default_content() try: ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) ifrArvoreHtml = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrArvoreHtml"))) self.driver.switch_to.frame(ifrArvoreHtml) documento_conteudo = self.driver.find_element_by_xpath( '/html/body').get_attribute('innerHTML') documento_conteudo = sub( r'\\n', '', documento_conteudo) # retirar quebra de páginas documento_conteudo = sub(r'\s\s+?', ' ', documento_conteudo) # tira espaços duplos documento_conteudo = sub(r' ', ' ', documento_conteudo) # tira espaços duplos documento_conteudo = documento_conteudo.strip( ) # retirar quebras de páginas que tenham restado return documento_conteudo except: raise Exception('Conteúdo do documento %s não encontrado.' % documento) finally: self.driver.switch_to.default_content() def get_documento_element_by_id(self, id, documento=None): if documento: self.go_to(documento) else: self.driver.switch_to.default_content() try: if (self.__windows_after == self.__windows_before): ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) ifrArvoreHtml = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrArvoreHtml"))) self.driver.switch_to.frame(ifrArvoreHtml) return self.driver.find_element_by_id(id).text except: raise Exception('Conteúdo do documento %s não encontrado.' % documento) finally: self.driver.switch_to.default_content() def get_documento_elements_by_id(self, id, documento=None): if documento: self.go_to(documento) else: self.driver.switch_to.default_content() try: if (self.__windows_after == self.__windows_before): ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) ifrArvoreHtml = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrArvoreHtml"))) self.driver.switch_to.frame(ifrArvoreHtml) elements = self.driver.find_elements_by_id(id) return [element.text for element in elements] except: raise Exception('Conteúdo do documento %s não encontrado.' % documento) finally: self.driver.switch_to.default_content() def get_documento_element_by_xpath(self, xpath, documento=None): if documento: self.go_to(documento) else: self.driver.switch_to.default_content() try: if (self.__windows_after == self.__windows_before): ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) ifrArvoreHtml = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrArvoreHtml"))) self.driver.switch_to.frame(ifrArvoreHtml) return self.driver.find_element_by_xpath(xpath).text except: raise Exception('Conteúdo do documento %s não encontrado.' % documento) finally: self.driver.switch_to.default_content() def get_documento_elements_by_xpath(self, xpath, documento=None): if documento: self.go_to(documento) else: self.driver.switch_to.default_content() try: if (self.__windows_after == self.__windows_before): ifrVisualizacao = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrVisualizacao"))) self.driver.switch_to.frame(ifrVisualizacao) ifrArvoreHtml = WebDriverWait(self.driver, 3).until( EC.presence_of_element_located((By.ID, "ifrArvoreHtml"))) self.driver.switch_to.frame(ifrArvoreHtml) elements = self.driver.find_elements_by_xpath(xpath) return [element.text for element in elements] except: raise Exception('Conteúdo do documento %s não encontrado.' % documento) finally: self.driver.switch_to.default_content() def close(self, voltar=True): if voltar: self.seleciona_area(self.__area_incial) self.driver.close() self.driver.quit()
def main(): try: print("ウェブドライバーを立ち上げています・・・") port = str(args.port[0]) load_delay_time = args.load_delay_time[0] options = EdgeOptions() options.use_chromium = True driver = Edge(options=options) driver.maximize_window() if len(port) != 4: print("入力した番号は4桁ではないです。4桁のポート番号を記入してください。") quit() print("ページを開いています・・・") driver.get(f"http://127.0.0.1:{port}") print(f"ページの読み込みのため{str(load_delay_time)}秒待機します・・・") for i in range(load_delay_time, 0, -1): time.sleep(1) print(f"終わるまで{i}秒") print("Interactive Pythonコンソールを立ち上げています・・・") soup = BeautifulSoup(driver.page_source, features="lxml") #Define web elements to be tested as dictionary where element ids are the keys. test_element_ids = { "dtFilter": { "tag": "select", "click_el_xpath": "/html/body/div/div[1]/div[2]/div/div/div[1]/div/div/div[1]/div[1]/div/div/div" }, "maxAmount": { "tag": "input", }, "maxSigma": { "tag": "input", }, "pl": { "tag": "select", "click_el_xpath": "/html/body/div/div[1]/div[2]/div/div/div[1]/div/div/div[1]/div[5]/div/div/div" }, "reason": { "tag": "select", "click_el_xpath": "/html/body/div/div[1]/div[2]/div/div/div[1]/div/div/div[1]/div[6]/div/div/div/div[1]" } } for test_el_id in test_element_ids: test_el = test_element_ids[test_el_id] if test_el["tag"] == "select": el = driver.find_element_by_xpath(test_el["click_el_xpath"]) el.click() soup = BeautifulSoup(driver.page_source, features="lxml") select_items = [ tag.text for tag in soup.find( id=test_el_id).find_next_sibling().select("div.option") ] print(f"number of items in select box: {len(select_items)}") for select_item in select_items: click_el = driver.find_element_by_css_selector( f"[data-value='{select_item}']") el.click() click_el.click() time.sleep(5) elif test_el["tag"] == "input": test_round = 1 while test_round < 6: test_input_number = int(random.random() * random.choice([ 10, 100, 1000, 10000, 100000, 1000000, 10000000, 10000000, 100000000 ])) el = driver.find_element_by_id(test_el_id) el.clear() el.click() el.send_keys(test_input_number) time.sleep(5) test_round += 1 el.clear() el.send_keys(0) except Exception as e: print( f"(EXCEPT) An error occurred: {str(e)} Attempting to enter debug mode at point of error." ) embed() finally: print("プログラムが正常終了しました。ウェブドライバーを終了します。お疲れ様でした。") embed() driver.close()
options.use_chromium = True driver = Edge(options=options) driver.get("https://www.youtube.com/") driver.maximize_window() time.sleep(1) driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") actions = driver.find_element_by_tag_name('body') actions.send_keys(Keys.TAB * 4, Keys.ENTER) time.sleep(1) actions = driver.find_element_by_tag_name('body') actions.send_keys(Keys.TAB * 3, Keys.ENTER) #actions.click() #actions.send_keys(Keys.ENTER) time.sleep(1) inputElement = driver.find_element_by_id("identifierId") inputElement.send_keys('*****@*****.**', Keys.ENTER) time.sleep(3) inputElement = driver.find_element_by_xpath( "/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input" ) inputElement.send_keys('passwordfraca', Keys.ENTER) time.sleep(1) driver.get( "https://www.youtube.com/results?search_query=Drive+Drive+Drive+song+(Impractical+Jokers)+-+2+HOUR+VERSION" ) time.sleep(1) inputElement = driver.find_element_by_xpath( "/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-video-renderer[1]" )