Answer the question
In order to leave comments, you need to log in
How to assign a value to a variable based on a condition?
Good afternoon.
I made a script for configuring cameras.
The settings may differ depending on the camera class.
It is required that at the beginning of the script I specify the camera class and, based on this, the necessary values \u200b\u200bare substituted in the settings. For example, if the class is 1, then the bitrate is 4000, if the class is 2, then the bitrate is 2000, in the example I changed the ip address of the ntp server.
Since I'm just starting to learn python, I didn't think of anything smarter than putting the whole body of settings into an instruction after the condition.
It works, but it's very cumbersome. I would be grateful if you could suggest a more elegant solution.
from selenium import webdriver
import time
ip = input('Введите ip')
ip = (f'http://{ip}')
type = input('Введите тип камеры')
type=int(type)
if type < 3:
browser=webdriver.Firefox()
browser.get(f'http://{ip}')
textarea=browser.find_element_by_css_selector('#username')
textarea.send_keys('admin')
textarea=browser.find_element_by_css_selector('#password')
textarea.send_keys('password')
submit=browser.find_element_by_css_selector('.btn')
submit.click()
time.sleep(5)
if type < 3:
browser=webdriver.Firefox()
browser.get(f'http://{ip}')
textarea=browser.find_element_by_css_selector('#username')
textarea.send_keys('admin')
textarea=browser.find_element_by_css_selector('#password')
textarea.send_keys('password')
submit=browser.find_element_by_css_selector('.btn')
submit.click()
time.sleep(5)
set=browser.find_element_by_css_selector('CSS-селектор')
set.click()
time.sleep(5)
system=browser.find_element_by_css_selector('CSS-селектор')
system.click()
ntp=browser.find_element_by_css_selector('CSS-селектор')
ntp.click()
time.sleep(2)
ntpip=browser.find_element_by_css_selector('CSS-селектор')
ntpip.click()
ntpip.clear()
ntpip.send_keys('10.0.0.1')
ntpport=browser.find_element_by_css_selector('CSS-селектор')
ntpport.click()
ntpport.clear()
ntpport.send_keys('1')
ntpinterval=browser.find_element_by_css_selector('CSS-селектор')
ntpinterval.click()
ntpinterval.clear()
ntpinterval.send_keys('60')
ntpsave=browser.find_element_by_css_selector('CSS-селектор')
ntpsave.click()
elif type == 3:
browser=webdriver.Firefox()
browser.get(f'http://{ip}')
textarea=browser.find_element_by_css_selector('#username')
textarea.send_keys('admin')
textarea=browser.find_element_by_css_selector('#password')
textarea.send_keys('password')
submit=browser.find_element_by_css_selector('.btn')
submit.click()
time.sleep(5)
set=browser.find_element_by_css_selector('CSS-селектор')
set.click()
time.sleep(5)
system=browser.find_element_by_css_selector('CSS-селектор')
system.click()
ntp=browser.find_element_by_css_selector('CSS-селектор')
ntp.click()
time.sleep(2)
ntpip=browser.find_element_by_css_selector('CSS-селектор')
ntpip.click()
ntpip.clear()
ntpip.send_keys('10.0.0.2')
ntpport=browser.find_element_by_css_selector('CSS-селектор')
ntpport.click()
ntpport.clear()
ntpport.send_keys('1')
ntpinterval=browser.find_element_by_css_selector('CSS-селектор')
ntpinterval.click()
ntpinterval.clear()
ntpinterval.send_keys('90')
ntpsave=browser.find_element_by_css_selector('CSS-селектор')
ntpsave.click()
else
browser=webdriver.Firefox()
browser.get(f'http://{ip}')
textarea=browser.find_element_by_css_selector('#username')
textarea.send_keys('admin')
textarea=browser.find_element_by_css_selector('#password')
textarea.send_keys('password')
submit=browser.find_element_by_css_selector('.btn')
submit.click()
time.sleep(5)
set=browser.find_element_by_css_selector('CSS-селектор')
set.click()
time.sleep(5)
system=browser.find_element_by_css_selector('CSS-селектор')
system.click()
ntp=browser.find_element_by_css_selector('CSS-селектор')
ntp.click()
time.sleep(2)
ntpip=browser.find_element_by_css_selector('CSS-селектор')
ntpip.click()
ntpip.clear()
ntpip.send_keys('10.0.0.3')
ntpport=browser.find_element_by_css_selector('CSS-селектор')
ntpport.click()
ntpport.clear()
ntpport.send_keys('1')
ntpinterval=browser.find_element_by_css_selector('CSS-селектор')
ntpinterval.click()
ntpinterval.clear()
ntpinterval.send_keys('120')
ntpsave=browser.find_element_by_css_selector('CSS-селектор')
ntpsave.click()
Answer the question
In order to leave comments, you need to log in
The repeating code should be formatted as a function, and the desired values should be passed through the function arguments.
By the way, selenium has its own expectation. Read about it. Well, in terms of design: our functions are everything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question