PYTHON里if

    xiaoxiao2021-03-25  19

    DSP_login_class.py

    from selenium import webdriver from time import * from selenium.common.exceptions import NoSuchElementException class Login(): def __init__(self): self.driver = webdriver.Firefox() self.url = "http://managerx.biddingx.com/#login" self.timeout = 5 def login_on(self): self.driver.get(self.url) self.driver.implicitly_wait(5) self.driver.find_element_by_xpath("//*[@id='LoginFormEmail']").clear self.driver.find_element_by_xpath("//*[@id='LoginFormEmail']").send_keys("1111@qq.com") self.driver.find_element_by_xpath("//*[@id='LoginFormPass']").clear() self.driver.find_element_by_xpath("//*[@id='LoginFormPass']").send_keys("Lihdh0") self.driver.find_element_by_xpath(".//*[@id='SCENES_LOGIN']/div/div/div[2]/div[4]/button").click() sleep(2) def login_off(self): try: el = self.driver.find_element_by_xpath(".//*[@id='SCENES_MAIN']/div/div[1]/div[2]/div[5]/div/div/i") el.click() el2 = self.driver.find_element_by_link_text("退出登录") except NoSuchElementException as e: print(e) else: el2.click() finally: self.driver.quit() if __name__ == "__main__": l = Login() l.login_on() l.login_off()

    直接运行这个脚本时if __name__ =="__main__":下的代码会被执行。

    引用登录木块.py

    from DSP_login_class import Login l = Login() #调用登录模块登录dsp l.login_on() #调用登录模块退出登录 l.login_off() 作为模块被调用,DSP_login_class中的if __name__=="__main__":不会被执行。

    if __name__ =="__main__":表示执行的是此代码所在的文件,如果这个文件作为模块被调用不会执行这里面的代码,只有运行这个文件时,if下的语句才会被执行。

    python 脚本中有些命令需要在手动执行时执行,但是在别的脚本import 模块时不执行,这时就用 if __name__ == "__main__" 进行控制。

    转载请注明原文地址: https://ju.6miu.com/read-300045.html

    最新回复(0)