1

I am executing my python script submit.py from another python script as follows :

response = os.popen("python3 static/submit.py "+contest_id+" "+task_id).read()
print(response)

But I am not able to find proper exit command to stop execution of script.

submit.py :

import sys
import os
import time
from credentials import USER_NAME, PASSWORD
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

contest_id = (sys.argv)[1]
task_id = (sys.argv)[2]

url = "https://codeforces.com/contest/"+contest_id+"/problem/"+task_id

try:
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    driver = webdriver.Chrome('chromedriver', chrome_options=chrome_options)
except:
    print("WEBDRIVER ERROR")
    # exit command

try:
    driver.get(url)
except:
    print("NETWORK ERROR / SITE UNAVAILABLE")
    # exit command

enter = driver.find_element(By.LINK_TEXT, "Enter")
enter.click()
user_name = driver.find_element_by_id("handleOrEmail")
user_name.send_keys(USER_NAME)
password = driver.find_element_by_id("password")
password.send_keys(PASSWORD)
submit = driver.find_element_by_class_name("submit")
submit.click()
time.sleep(10)
try:
    error_source_file = driver.find_element_by_class_name("shiftUp")
    print("INVALID CREDENTIALS")
    # exit command
except:
    pass

language = driver.find_element_by_name("programTypeId")
language.send_keys("GNU G++17 7.3.0")
sourceFile = driver.find_element_by_name("sourceFile")
sourceFile.send_keys(os.path.abspath("contests/cf_"+str(contest_id)+"/"+task_id+".cpp"))
submit_btn = driver.find_element_by_class_name("submit")
submit_btn.click()
try:
    error_source_file = driver.find_element_by_class_name("shiftUp")
    print("REPEATED CODE SUBMISSION")
except:
    print("SOLUTION SUBMITTED")

driver.close()

At each position of comment exit command( # exit command ), I need proper exit command to place.

UPDATE : With wrong credentials, I tried sys.exit() as exit command, it throws error :

Traceback (most recent call last):
  File "static/submit.py", line 44, in <module>
    language = driver.find_element_by_name("programTypeId")
  File "/home/pk/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 496, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "/home/pk/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/home/pk/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/pk/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"programTypeId"}
  (Session info: headless chrome=75.0.3770.90)
  (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.15.0-50-generic x86_64)
INVALID CREDENTIALS

Last line is response returned. So, it can be seen that script execution does not stop at exit command.

1
  • Don't use except: without raise, it will hide bugs Commented Jun 16, 2019 at 17:13

1 Answer 1

2

Since you're importing sys, you can simply use exit() to end the program.

Code:

sys.exit()

Update: It's working for me with no problems. Try this code and see by yourself.

import sys

if sys.argv[1] == 'x':
    print("Im exiting now!")
    sys.exit()
else:
    print("Im not exiting")

print("Here is another string")
Sign up to request clarification or add additional context in comments.

6 Comments

I tried with invalid credentials. So, response is coming out to be "INVALID CREDENTIALS" as exepected, but it is also throwing an error on line 44 to get element of 'programTypeId'. So, I think sys.exit() is not working.
If driver.find_element_by_class_name("shiftUp") is throwing the exception then you are catching that so sys.exit() will never be called. You need to change your logic.
Did you try to run the file submit.py from the console instead of another file?
I want that only. If "shiftUp" class is present, then only it should exit, otherwise continue execution.
Yes, I tried it from the console. It is returning correct response with same error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.