Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
| knowledge_base:programming:selenium [2022/12/02 15:58] – removed - external edit (Unknown date) 127.0.0.1 | knowledge_base:programming:selenium [2022/12/02 15:58] (current) – ↷ Page moved from knowledge_base:home_it:selenium to knowledge_base:programming:selenium George Wayne | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | # | ||
| + | # This is writen and tested under python2.7 and | ||
| + | # it should be able to adapt to python3 with minimum changes. | ||
| + | # Chrome browser is required | ||
| + | # The script was tested on a Mac machine. Please change the driver if | ||
| + | # your environment is Linux or Windows. Driver can be found at | ||
| + | # | ||
| + | |||
| + | # Usage: | ||
| + | # | ||
| + | # where 100 is the number of entries you want to parse. Any interger is OK. | ||
| + | |||
| + | from selenium import webdriver | ||
| + | import datetime | ||
| + | import time | ||
| + | import os | ||
| + | import csv | ||
| + | import sys | ||
| + | |||
| + | parent_diretory_path = os.path.abspath(__file__+"/ | ||
| + | Result_directory_path = os.path.abspath(__file__+"/ | ||
| + | |||
| + | # This is tested on a Mac machine. Change the following driver to your computer OS system. Drives are located at: | ||
| + | # https:// | ||
| + | driver = webdriver.Chrome(parent_diretory_path+'/ | ||
| + | |||
| + | driver.get(" | ||
| + | |||
| + | result_file_name = ' | ||
| + | result_folder = datetime.datetime.now().strftime(" | ||
| + | |||
| + | def write_result_csv(row): | ||
| + | result_file = open(Result_directory_path + '/' | ||
| + | csv_writer = csv.writer(result_file) | ||
| + | csv_writer.writerow(row) | ||
| + | result_file.close() | ||
| + | |||
| + | |||
| + | def createresultfolder(): | ||
| + | try: | ||
| + | os.makedirs(Result_directory_path + '/' | ||
| + | except: | ||
| + | print(" | ||
| + | result_file = open(Result_directory_path + '/' | ||
| + | csv_writer = csv.writer(result_file) | ||
| + | csv_writer.writerow( | ||
| + | [' | ||
| + | result_file.close() | ||
| + | |||
| + | |||
| + | def login(username, | ||
| + | try: | ||
| + | driver.find_element_by_xpath('// | ||
| + | driver.find_element_by_xpath('// | ||
| + | time.sleep(1) | ||
| + | driver.find_element_by_xpath('// | ||
| + | time.sleep(1) | ||
| + | except: | ||
| + | print(" | ||
| + | |||
| + | def parseHistoryData(): | ||
| + | time.sleep(3) | ||
| + | datetimestamp = driver.find_element_by_xpath("// | ||
| + | question = driver.find_element_by_xpath('// | ||
| + | ttsreponse= "" | ||
| + | try: | ||
| + | ttsreponse = driver.find_element_by_xpath('// | ||
| + | except: | ||
| + | try: | ||
| + | ttsreponse = driver.find_element_by_xpath('// | ||
| + | except: | ||
| + | ttsreponse = " | ||
| + | |||
| + | print(datetimestamp, | ||
| + | write_result_csv([str(datetimestamp), | ||
| + | |||
| + | |||
| + | def toHistoryPage(count): | ||
| + | time.sleep(3) | ||
| + | |||
| + | #opening the history page url | ||
| + | driver.get(' | ||
| + | time.sleep(3) | ||
| + | |||
| + | for i in range(count): | ||
| + | dl = i / 25 + 1 | ||
| + | dd = i%25 +1 | ||
| + | |||
| + | xpath = "// | ||
| + | element = driver.find_element_by_xpath(xpath) | ||
| + | driver.execute_script(" | ||
| + | element.click() | ||
| + | time.sleep(2) | ||
| + | |||
| + | parseHistoryData() | ||
| + | |||
| + | driver.get(' | ||
| + | |||
| + | time.sleep(3) | ||
| + | |||
| + | for line in range(1, | ||
| + | xpath = "// | ||
| + | element = driver.find_element_by_xpath(xpath) | ||
| + | driver.execute_script(" | ||
| + | time.sleep(1) | ||
| + | |||
| + | if __name__ == " | ||
| + | |||
| + | username = str(sys.argv[1]) | ||
| + | password = str(sys.argv[2]) | ||
| + | count = int(sys.argv[3]) | ||
| + | |||
| + | try: | ||
| + | createresultfolder() | ||
| + | login(username, | ||
| + | toHistoryPage(count) | ||
| + | driver.quit() | ||
| + | print(" | ||
| + | except: | ||
| + | print(" | ||
| + | |||
| + | </ | ||