ihue ihue - 7 months ago 5
Python Question

Execute Selenium Python Code in a For Loop

I have a script call create_cpe.py
It open up firefox and create a cpe.




create_cpe.py



# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import random
import requests

class CreateCPE(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://localhost:8888"
self.verificationErrors = []
self.accept_next_alert = True

def test_create_c_p_e(self):

mac = [ 0x00, 0x24, 0x81,
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff) ]
cpe_mac = ':'.join(map(lambda x: "%02x" % x, mac))
cpe_mac = cpe_mac.replace(":","").upper()
rand_cpe_name = "Bunlong's AP " + str(random.randint(2,99))

url = "https://randomuser.me/api/"
data = requests.get(url).json()

add1 = data['results'][0]['location']['street']
city = data['results'][0]['location']['city']
state = data['results'][0]['location']['state']
postcode = data['results'][0]['location']['postcode']


print rand_cpe_name
print cpe_mac

driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("username").send_keys("admin@benunets.com")
driver.find_element_by_id("password").send_keys("admin")
driver.find_element_by_xpath("//button[@type='submit']").click()
driver.get(self.base_url + "/account/1001/cpe/create")
driver.find_element_by_css_selector(".add-location").click()
driver.find_element_by_name("add1").send_keys(add1)
driver.find_element_by_name("add2").send_keys("")
driver.find_element_by_name("city").send_keys(city)
driver.find_element_by_name("state").send_keys(state)
driver.find_element_by_name("zip").send_keys(postcode)
driver.find_element_by_name("country").send_keys("USA")
driver.find_element_by_css_selector(".btn.btn-primary").click()
driver.find_element_by_name("mac").send_keys(cpe_mac)
driver.find_element_by_name("cpe_name").send_keys(rand_cpe_name)
driver.find_element_by_name("p_ip").send_keys("192.168.9.2")
driver.find_element_by_name("p_netmask").send_keys("255.255.255.0")
driver.find_element_by_name("p_max_user").send_keys("50")
driver.find_element_by_name("dns").send_keys("172.16.0.73")
driver.find_element_by_name("dns2").send_keys("172.16.0.74")
Select(driver.find_element_by_name("g_max_up")).select_by_visible_text("256 Kbps")
Select(driver.find_element_by_name("g_max_down")).select_by_visible_text("2000 Kbps")
driver.find_element_by_name("g_dns2").send_keys("172.16.0.74")
driver.find_element_by_name("g_portal").send_keys("http://www.bunlongheng.com")
driver.find_element_by_name("g_netmask").send_keys("255.255.255.0")
driver.find_element_by_name("g_max_user").send_keys("40")
driver.find_element_by_name("g_dns").send_keys("172.16.0.74")
driver.find_element_by_name("dns2").send_keys("172.16.0.75")
driver.find_element_by_name("g_dns2").send_keys("172.16.0.76")
driver.find_element_by_name("g_ip").send_keys("192.168.9.3")
driver.find_element_by_css_selector("button.btn.btn-success").click()

def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True

def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True

def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True

def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
unittest.main()


It works when I run
python create_cpe.py





Now



I'm take it to the next level.
I'm trying to create a for loop and run that same script

import os
cpe = raw_input("How many CPE you want to create ? : ")
for x in xrange(int(cpe)):
os.system("/Applications/MAMP/htdocs/code/python/create_cpe.py")

print cpe + ' new CPE(s) inserted !'


and I got

from: can't read /var/mail/selenium
from: can't read /var/mail/selenium.webdriver.common.by
from: can't read /var/mail/selenium.webdriver.common.keys
from: can't read /var/mail/selenium.webdriver.support.ui
from: can't read /var/mail/selenium.common.exceptions
from: can't read /var/mail/selenium.common.exceptions
/Applications/MAMP/htdocs/code/python/create_cpe.py: line 8: import: command not found
/Applications/MAMP/htdocs/code/python/create_cpe.py: line 9: import: command not found
/Applications/MAMP/htdocs/code/python/create_cpe.py: line 10: import: command not found
/Applications/MAMP/htdocs/code/python/create_cpe.py: line 12: syntax error near unexpected token `('
/Applications/MAMP/htdocs/code/python/create_cpe.py: line 12: `class CreateCPE(unittest.TestCase):'





Did I missed anything ?

Should I try something elses ?

Why it is not working when I use the for loop to execute it ?

Answer

os.system is running it like it's a bash script. The line that runs it should be

os.system("python /Applications/MAMP/htdocs/code/python/create_cpe.py")
Comments