Python Selenium CPPSecrets article poster














































Python Selenium CPPSecrets article poster



Python, since its inception, is known for its interpreted language and ability to perform variety of tasks, starting from simplest "Hello World", to developing websites using Django and Flask. One another use of python, among other functionalities, is website testing. For this purpose, we have Selenium. Selenium is a framework for web automation, at its very core. So, we take a look at selenium to make a script that automates the job of posting articles to this very site. (This very article is posted using same script, just to test it is working or not.)

What I left out: Exception handling and DriverWait for the page to load.

CODE:
from selenium import webdriver from selenium.webdriver.common.keys import Keys import getpass driver = webdriver.Firefox() driver.set_window_size(900, 600) driver.get('https://cppsecrets.com') user = input('Enter Username: ') passw = getpass.getpass('Enter Password: ') categories = {'C++':2,'C++ Algorithms':3,'Python':4,'Python Django':5,
'GDB':6,'Linux':7,'Tech News':8,'Other':9} driver.find_element_by_name('email').send_keys(user) driver.find_element_by_name('password').send_keys(passw) driver.find_element_by_name('login').click() driver.find_element_by_xpath("//*[@id='home']/div/div[1]/section/div/div[2]/button").click() heading = input('Give a heading: ') driver.find_element_by_id('heading0').send_keys(heading) category = input('Give a category: ') for key,val in categories.items(): if key == category: path = "/html/body/div[1]/div/div/section/div[1]/div[1]/div/abc/select/option[{}]".format(val) driver.find_element_by_xpath(path).click() break file_path = input('Enter content file path: ') content = driver.find_element_by_xpath("//*[@id='myArea1x']/div[2]/div") content.send_keys(Keys.CONTROL + 'a') content.send_keys(Keys.DELETE) article = open(file_path, "r") content.send_keys(article.read()) ch = input('Does this article need a code? [y/n]: ') if ch.lower() == 'y': code_file = input('Enter path of code file: ') driver.execute_script("window.open('');") driver.switch_to.window(driver.window_handles[1]) driver.get('https://cppsecrets.com/code_formatter/') code = open(code_file, "r") driver.find_element_by_xpath("//*[@id='codeText']").send_keys(code.read()) driver.find_element_by_xpath("/html/body/div/button").click() output = driver.find_element_by_xpath("//*[@id='out']").get_attribute("outerHTML") driver.close() driver.switch_to.window(driver.window_handles[0]) driver.find_element_by_xpath("//*[@id='myArea1x']/div[1]/div/div/div[27]/div/div/div").click() driver.find_element_by_xpath("//*[@id='code']").send_keys(output) driver.find_element_by_xpath("/html/body/div[4]/div/form/input").click() input("Submit Article ?") print("Submitting Article...") driver.find_element_by_xpath("//*[@id='submitArticle']").click() input("Exit?") driver.find_element_by_xpath("/html/body/header/nav/div/div[2]/ul/li[2]/a").click() article.close() code.close() driver.quit()

Comments