0

for example, I run this script 200 times and now I want to run more time. then I count every time and show me in console

import time


from selenium import webdriver
start_time = time.time()
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://aujtravels.com/broadway3/")
print(driver.current_url)
print(driver.title)
driver.maximize_window()
driver.implicitly_wait(5)
driver.find_element_by_link_text("Signup").click()
time.sleep(2)
driver.find_element_by_xpath("//input[@placeholder='Enter Your name']").send_keys("arslan")
driver.find_element_by_xpath("//input[contains(@placeholder,'Enter Phone Number')]").send_keys("arslan.hassan@htmlpro.net")
driver.find_element_by_id("password").send_keys("123456")
driver.find_element_by_name("confirm_password").send_keys("123456")
driver.find_element_by_css_selector("input[placeholder='Enter Your email']").send_keys("hr@gmail.com")
driver.find_element_by_tag_name("textarea").send_keys("hi am new here ")
driver.find_element_by_css_selector("button[class='theme-btn btn-style-one place-order']").click()
print(driver.find_element_by_xpath("//div[@class='toast-message']").text)
time.sleep(1)
driver.find_element_by_link_text("Forgot Password").click()
driver.find_element_by_id("email").send_keys("hr@gmail.com")
driver.find_element_by_link_text("Login").click()
time.sleep(1)
driver.find_element_by_xpath("//input[@placeholder='Enter Your email']").send_keys("hr@gmail.com")
driver.find_element_by_xpath("//input[@name='password']").send_keys("123456")
driver.find_element_by_xpath("//button[contains(text(),'Login')]").click()
print(driver.find_element_by_xpath("//div[contains(text(),'Login Successfully!')]").text)

time.sleep(2)
print("***********************Scrolled down starts***************")
for i in range(10):
    time.sleep(0.5)
    driver.execute_script(" window.scrollBy(0, 350);")
    print("scrolling time: " + str(i))
print("*************************scrolling down end**********************")
print("**************************************Scrolled up starts****************")
for i in range(10):
    time.sleep(0.5)
    driver.execute_script(" window.scrollBy(0,-350);")
    print("scrolling time: - " + str(i))
print("**************************************Scrolled up End ****************")

time.sleep(1)
driver.find_element_by_css_selector("[class='icon fa fa-search']").click()
time.sleep(1)
driver.find_element_by_xpath("//input[@name='keyword']").send_keys("hp")
driver.find_element_by_xpath("//input[@value='Search']").click()
time.sleep(1)
driver.execute_script("window.scrollTo(0,150);")
time.sleep(1)
driver.find_element_by_xpath("//a[contains(text(),'Add to cart')]").click()
time.sleep(2)
driver.find_element_by_name("quantity").click()
driver.find_element_by_name("quantity").clear()
driver.find_element_by_name("quantity").send_keys("2")
driver.find_element_by_css_selector("[class='theme-btn btn-style-one add-to-cart']").click()
print(driver.find_element_by_xpath("//div[@class='toast-message']").text)
time.sleep(5)
driver.find_element_by_xpath("//i[@class='fa ']").click()
time.sleep(1)
driver.find_element_by_link_text("Proceed to checkout").click()
driver.execute_script("window.scrollTo(0,400);")
time.sleep(2)
driver.find_element_by_css_selector("#shipping_address").send_keys("Lahore")
driver.find_element_by_id("billing_address").send_keys("infinit connection ")

a = driver.find_element_by_css_selector("tbody tr:nth-child(1) td:nth-child(1)").text
print("Product Title =" + str(a))

b = driver.find_element_by_css_selector("td:nth-child(2) ").text
print("Shipping Cost =" + str(b))

c = driver.find_element_by_xpath("//td[@class='price']").text
print("Price =" + str(c))

d = driver.find_element_by_xpath("//td[normalize-space()='2']").text
print("Quantity =" + str(d))

OrignalAmount  = driver.find_element_by_xpath("//td[@class='sub-total']").text
print("SUBTOTAL =" + str(OrignalAmount))

# f= driver.find_element_by_xpath("//*[@name='dis']").text
# print("Discount Applied =" + str(f))

DiscountAmount = driver.find_element_by_xpath("//input[@value='$ 1500.00']").text
print("Net Total = " + str(DiscountAmount))
# Asseration
assert DiscountAmount > OrignalAmount

print(driver.find_element_by_xpath("//h2[contains(text(),'Payment Method')]").text)
print(driver.find_element_by_xpath("//strong[contains(text(),'Cash')]").text)
cash = driver.find_element_by_xpath("//strong[contains(text(),'Cash')]").click()

driver.find_element_by_css_selector("[class='process-checkout theme-btn btn-style-one place-order']").click()




I have used this method but in console, it always shows 1 rather than increment number kindly help


import pipes
import os
value = int(os.environ['MY_COUNTER']) + 1 if 'MY_COUNTER' in os.environ else 1
print("export MY_COUNTER=%s" % (pipes.quote(str(value))))     
1
  • I have used one function that counts my script starting and ending time but I want to count the number of times of running the script . Mar 16 at 6:47
2

It will depend if where you want to put the responsibility of this tracking.

Ultimately, you will have to keep track of the number of runs in the environment where you invoke the script (e.g., your shell) or, in the script, update a file where you keep track of the number of runs. In the second case, the tracking is independent of the way you run.

4
  • Farias have you any code that I put in the script Mar 16 at 7:00
  • I guess the main verb of your phrase is missing :D Mar 16 at 7:11
  • kindly leave my verb phrase and tell me if you have any code that helpful for me Mar 16 at 7:18
  • I mentioned it because your phrase didn't have meaning, I couldn't understand. I guess you meant "do you have any code". As I mentioned, if you want to do the counting in the script itself, you can start off by having a file with a zero - at the end of each execution, you open this file and increment the value. Mar 16 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.