Guest User

Untitled

a guest
Jan 28th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #Install selenium with pip install selenium
  2. #and make sure you place Chromedriver on the code path and latest Chrome Installed
  3.  
  4. import selenium
  5. #used for sending keys
  6. from selenium.webdriver.common.keys import Keys
  7. #Used for Waiting for a xapth to be available
  8. from selenium.webdriver.support.ui import WebDriverWait
  9. from selenium.webdriver.support import expected_conditions as EC
  10. from selenium.webdriver.common.by import By
  11.  
  12.  
  13. #Initialize
  14. driver = webdriver.Chrome()
  15. URL= "www.amazon.in"
  16.  
  17. #opening URL
  18. driver.get(URL)
  19. #finding element using xpath
  20. xpath_username = "//input[@id='username']" # 1. //- syntax find all elem , 2. input - tag , 3. @id - property , 4. "username" matching string
  21. xpath_password = "//input[@id='password']"
  22. xpath_login = "//button[@id='submit']"
  23. xpath_list = "//li[@class='menu']"
  24. driver.find_element_by_xpath(xpath_username)
  25.  
  26. #Sending input data , sending to username field of the website. make sure Xpath is correctly mapping to input field
  27. driver.find_element_by_xpath(xpath_username).send_keys("arunkarnan")
  28. driver.find_element_by_xpath(xpath_password).send_keys("#########")
  29. #Clicking button
  30. driver.find_element_by_xpath(xpath_login).click()
  31.  
  32. #Sometime we need to wait for JS to include certain elements or tags so we use Wait Method for it
  33. #Below code will wait until the page loas and shows username field "20" is timeout(Hot long to wait).
  34. WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,xpath_username)))
  35.  
  36. #You can find list of all elements using find_element<s>_by_xpath , this will return <web element> list
  37. driver.find_element_by_xpath(xpath_list)
  38.  
  39. #YOu can get an elements attributes value by get_attribute('href') method with attribute key as parameters
  40. driver.find_element_by_xpath("//img").get_attribute('href')
  41.  
  42. #Finally close the driver before finish otherwise I will keep on running in bg
  43. driver.close()
Add Comment
Please, Sign In to add comment