Last active
January 4, 2016 05:49
-
-
Save Martin91/8577217 to your computer and use it in GitHub Desktop.
Retrieve tracking ids for Amazon associates through selenium web driver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'selenium-webdriver' | |
require 'csv' | |
# require 'pry' | |
ASSOCIATE_ACCOUNT = { email: '[email protected]', password: 'password' } | |
driver = Selenium::WebDriver.for :firefox | |
driver.navigate.to 'https://affiliate-program.amazon.com/' | |
# Fill in the account's email | |
driver.find_element(:id, 'username').send_keys ASSOCIATE_ACCOUNT[:email] | |
# Fill in the account's password | |
driver.find_element(:id, 'password').send_keys ASSOCIATE_ACCOUNT[:password] | |
driver.find_element(:name, 'sign_in').submit | |
driver.find_element(:id, 'manage_tracking_ids').click | |
tracking_list = driver.find_element(:class, 'tracking_id_list') | |
tracking_ids = tracking_list.find_elements(:tag_name, 'li').map(&:text) | |
puts tracking_ids | |
puts "Total ids: #{tracking_ids.count}" | |
csv_file_name = "tracking_ids_#{Time.now.to_i}" | |
puts "Writing to the csv file #{csv_file_name}" | |
CSV.open csv_file_name, 'wb' do |csv| | |
tracking_ids.each do |tracking_id| | |
csv << [tracking_id] | |
end | |
end | |
puts "Wrote to the file successfully!" | |
# Lastly signout and close the window | |
driver.find_element(:link_text, 'sign out').click | |
driver.quit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment