Recently I’ve been involved with some coolest people from IIT Bombay, IIT Delhi and other tech entrepreneurs from India in various projects. I am the youngest in the loop and it’s great to get involved in discussions with experts and computer wizards. In my article today, I will be displaying my part of contribution in a project which I really enjoyed doing. We will take a closer look at selenium module in python for automation while being in the premises of one of the rare websites making it almost impossible for automation, DropBox. DropBox has a mechanism of assigning dynamic id’s to elements on page load which confirms mechanize module can’t do the job for us. Therefore, we rely on selenium. Wait, there’s some more obstacles further. DropBox assigns id’s dynamically and these id’s are unique on every page load. By the end of this read, you will have a deeper understanding of selenium module and hopefully geeky way to tackle problems.
Python script for automating signups for DropBox
from selenium import webdriver from selenium.webdriver.common.keys import Keys browser= webdriver.Firefox() browser.get("https://dropbox.com/register") list_of_inputs = browser.find_elements_by_xpath("//div/input[starts-with(@id, 'pyxl')]") id_for_fname = str(list_of_inputs.get_attribute("id")) id_for_lname = str(list_of_inputs.get_attribute("id")) id_for_email = str(list_of_inputs.get_attribute("id")) id_for_password = str(list_of_inputs.get_attribute("id")) id_for_agreement = str(list_of_inputs.get_attribute("id")) fname = browser.find_element_by_id(id_for_fname) fname.send_keys("First Name") lname = browser.find_element_by_id(id_for_lname) lname.send_keys("Last Name") email = browser.find_element_by_id(id_for_email) email.send_keys("firstname.lastname@example.org") password = browser.find_element_by_id(id_for_password) password.send_keys("Strongpassword") agreement = browser.find_element_by_id(id_for_agreement) agreement.click() password.send_keys(Keys.RETURN)
Line to line breakdown – Python codes for automating sign ups using selenium python
Line 1 and 2
These are the import statements required for our program. webdriver is used to instantiate a browser as well as contains methods such as find_element_by_name and many more we will use later. Next, we import Keys to send keystrokes to the browser such as “RETURN”.
2. Line 4 and 5
Line 4 instantiates a firefox browser. For linux distro, we do not need to pass additional parameters to webdriver.Firefox() while it is necessary to pass the path to the browser in windows operating system. The get method on browser object opens the given parameter on the browser. The parameter to this method is essentially a web url.
3. Line 7
As mentioned earlier, dropbox has a mechanism to assign id’s to elements dynamically at load and the id is unique on each page load. If you closely look to the elements by inspecting them, you will notice that the id’s start with pyxl followed by numbers. The browser.find_elements_by_xpath(“//div/input[starts-with(@id, ‘pyxl’)]”) returns a list of input fields which is wrapped inside div tags and who’s id starts with ‘pyxl’ followed by any alphanumeric characters.
4. Line 9
The list we got from line 7 is of length 11 which contains different input fields while the one’s we need are in a sequence starting from index 7. The 7th index in the list is the input field for first name. Here the .get_attribute method takes a parameter of the attribute name (eg: id, name) to return the attribute of the current selenium element/object.
5. Line 11, 13, 15, 17
These are similar to the explanation on point 4. Reference back. These are the input fields for last name, email, password and a checkbox for license agreement.
6. Line 19 and 20
In line 19, we’ve used browser.find_element_by_id() method where we pass the id which we’ve got from in line 9. This selects the input element. Line 20 uses .sen_keys() method to pass the text or keystrokes to the input field(text in our case).
7. Line 22, 23 & 25,26 & 28,29 & 31,32
These are similar to what is explained in the point 6 above except for line 31 and 32. Well line 31 is the same but since the element selected is a checkbox we apply .click() method instead of .send_keys() to check mark the field.
8. Line 34
As mentioned above the .send_keys() method takes either text or keystrokes as defined inside Keys which we’ve imported in our program. For Linux and Windows the enter/return keystroke is passed via Keys.RETURN as a parameter to the .send_keys() method. Here we’ve selected password field and then called .send_keys() method on it. You can replace any input field instead. Just a way to get the signup form submitted in an easy way instead of clicking the submit button by looking for it.
Minified version of the program
Below is the minified version of the program reducing some statements which were intentionally added in the program above to improve the understanding of the selenium module.
from selenium import webdriver from selenium.webdriver.common.keys import Keys browser= webdriver.Firefox() browser.get("https://dropbox.com/register") list_of_inputs = browser.find_elements_by_xpath("//div/input[starts-with(@id, 'pyxl')]") list_of_inputs.send_keys("first name") list_of_inputs.send_keys("last name") list_of_inputs.send_keys("email") list_of_inputs.send_keys("password") list_of_inputs.click() password.send_keys(Keys.RETURN)
We’ve used the above program to get enough could space at dropbox. Hopefully I will write my next on how to get DropBox file access API via programming. If you have any questions regarding the article or the codes, comment below so we can discuss about it. Also let me know how you felt the article was so I can come up with fresh and useful content every week.