Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.common.keys import Keys
  4. from selenium.webdriver.support.ui import Select
  5. import unittest, time, re
  6.  
  7. self.driver = webdriver.PhantomJS()
  8. self.driver.implicitly_wait(30)
  9. self.base_url = "https://login.example.com"
  10.  
  11. driver = self.driver
  12. driver.get(self.base_url)
  13.  
  14. all_cookies = self.driver.get_cookies()
  15.  
  16. # It prints out all cookies and values just fine
  17. for cookie in all_cookies
  18. print cookie['name'] + " --> " + cookies['value']
  19.  
  20. # Set cookies to driver
  21. for s_cookie in all_cookies:
  22. c = { s_cookie['name'] : s_cookie['value']}
  23. # This is where it's throwing an error saying "Can only set Cookies for current domain
  24. driver.add_cookie(c)
  25.  
  26. ...
  27.  
  28. import unittest
  29. from selenium.webdriver.common.by import By
  30.  
  31. from selenium import webdriver
  32.  
  33.  
  34. __author__ = 'Saifur'
  35.  
  36.  
  37. class CookieManagerTest(unittest.TestCase):
  38. def setUp(self):
  39. self.driver = webdriver.PhantomJS("E:\working\selenium.python\selenium\resources\phantomjs.exe")
  40. self.driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/")
  41. self.driver.find_element(By.ID, "Email").send_keys("userid")
  42. self.driver.find_element(By.ID, "next").click()
  43. self.driver.find_element(By.ID, "Passwd").send_keys("supersimplepassword")
  44. self.driver.find_element(By.CSS_SELECTOR, "[type='submit'][value='Sign in']").click()
  45. self.driver.maximize_window()
  46.  
  47. def test(self):
  48. driver = self.driver
  49. listcookies = driver.get_cookies()
  50.  
  51. for s_cookie in listcookies:
  52. # this is what you are doing
  53. c = {s_cookie['name']: s_cookie['value']}
  54. print("*****The partial cookie info you are doing*****n")
  55. print(c)
  56. # Should be done
  57. print("The Full Cookie including domain and expiry infon")
  58. print(s_cookie)
  59. # driver.add_cookie(s_cookie)
  60.  
  61.  
  62. def tearDown(self):
  63. self.driver.quit()
  64.  
  65. url = 'http://domainname.com'
  66. url2 = 'http://domainname.com/page'
  67. USER = 'superAwesomeRobot'
  68. PASS = 'superSecretRobot'
  69.  
  70. # initiates your browser
  71. driver = webdriver.PhantomJS()
  72. # browses to your desired URL
  73. driver.get(url)
  74. # searches for the user or email field on the page, and inputs USER
  75. driver.find_element_by_id("email").send_keys(USER)
  76. # searches for the password field on the page, and inputs PASS
  77. driver.find_element_by_id("pass").send_keys(PASS)
  78. # finds the login button and click you're in
  79. driver.find_element_by_id("loginbutton").click()
  80.  
  81. driver.get(url2)
  82.  
  83. driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement