cfabio

Webbrowser.py

Jan 17th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. #open a jira isse in the browser
  2. import webbrowser
  3. import sys
  4. import requests
  5. import pyperclip
  6.  
  7. #get the issue number from the clipboard or in input
  8. if len(sys.argv) == 2 :
  9.     issue = sys.argv[1]
  10. else :
  11.     issue = pyperclip.paste()       #get the content of the clipboard
  12.  
  13.  
  14. sURL = r'https://jira.boc-group.com/browse/' + issue
  15. webbrowser.open_new(sURL)
  16.  
  17. ########################
  18. # get the page we want do download
  19. res = requests.get(sURL)
  20. res.raise_for_status() #exception if the status of the answer from the web is not OK
  21.  
  22. print(res.status_code) #verify status of the answer to the request
  23.  
  24. #contet of the page
  25. #print(res.text)
  26.  
  27. #to parse the HTML in a page retrieved with the request module
  28. #allows to retrieve a specific info from a website
  29. soup = bs4.beautifulsoup4(res.text, 'html.parser')
  30. #the lement is the one we can find in the browser with select element and after copy css path
  31. elems = soup.select(sElement)
  32. #get the text of the element and string all the white spaces
  33. elems[0].text.strip()
  34.  
  35.  
  36. #######################
  37. #Selenium start a browser that can be controlled by python, not just getting a page and parsing it
  38. #but actually executing actions with the elements we retrn
  39. #######################
  40. from selenium import webdriver
  41. browser = webdriver.Firefox()  #start firefox
  42. browser.get(sURL)
  43. elem = browser.find_element_by_css_selector(sElement) #one of the many getters for HTML elements
  44. elem.click()
  45.  
  46. searchElem = browser.find_element_by_css_selector(sElement)
  47. searchElem.send_keys('fabio')
  48. searchElem.submit()  #not necessary to actually find the button and click on okay. Selenium understands which is the form and automatically does it
  49.  
  50. browser.back()
  51. browser.forward()
  52. browser.refresh()
  53. browser.quit()
  54.  
  55. #to simply read an HTML element
  56. searchElem = browser.find_element_by_css_selector(sElement)
  57. searchElem.text
  58.  
  59. searchElem = browser.find_element_by_css_selector('html')  #get the whole page
Advertisement
Add Comment
Please, Sign In to add comment