Execute Selenium Python Code in a For Loop -
i have script call create_cpe.py open firefox , create cpe.
create_cpe.py
# -*- coding: utf-8 -*- selenium import webdriver selenium.webdriver.common.by import selenium.webdriver.common.keys import keys selenium.webdriver.support.ui import select selenium.common.exceptions import nosuchelementexception 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 e: return false return true def is_alert_present(self): try: self.driver.switch_to_alert() except noalertpresentexception 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 run python create_cpe.py
now
i'm take next level. i'm trying create loop , run same script
import os cpe = raw_input("how many cpe want create ? : ") x in xrange(int(cpe)): os.system("/applications/mamp/htdocs/code/python/create_cpe.py") print cpe + ' new cpe(s) inserted !'
and 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 missed ?
should try elses ?
why not working when use loop execute ?
os.system running it's bash script. line runs should be
os.system("python /applications/mamp/htdocs/code/python/create_cpe.py")
Comments
Post a Comment