当前位置:网站首页>Web automation: summary of special scenario processing methods

Web automation: summary of special scenario processing methods

2022-06-24 21:05:00 gchh_ spring

web Automate special scenarios :

1、 Browser reuse scenarios (chrome browser )

2、 Use cookie Complete the automatic login of the page

3、

Browser reuse scenarios (chrome browser )

Browser reuse steps :

1、 Get into chrome The installation path of the browser , And open in this path cmd Command line , Enter the following command :

     chrome --remote-debugging-port=9222

    This command is equivalent to opening chrome A debugging web page , Don't close pages opened in this way ,

2、 adopt python The code implements the direct reuse steps when the browser is opened 1 Debugging web page opened in , Code in definition webdriver Variable passed in options Parameters

chrome_args = webdriver.ChromeOptions()
chrome_args.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome(options=chrome_args)

Be careful : Before executing the code, you need to put all chrome Web page closed , Then reopen the debug page , Then run the reusable browser code ; If you have previously opened a non debug chrome Webpage , It will cause the code to get stuck

3、 If your web page needs to scan the code to log in , Then you can login in the debug browser web page first , Use steps 2 After reusing the browser , You can directly locate elements , So you can omit the login step

Use cookie Complete the automatic login of the page

cookie: It is a data cache or identity of the browser , stay cookie Without failure , The server can use the browser's cookie Data realizes the automatic login of users ,cookie The validity of is determined by the server

selenium Provides a way to get the current page directly cookie Method of information , Just take what you get cookie Data saved , Then reuse the cookie Can realize the automatic login of the web page

1、 First, log in to the web page by yourself , Through the code to get the web page cookie Information , And save to json In file

class TestLogin:
    def setup_method(self):
        chrome_args = webdriver.ChromeOptions()
        chrome_args.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome(options=chrome_args)

    def test_getcookies(self):
        #  Use the browser to debug the web page after logging in , Get the web page directly cookie Information 
        cookies = self.driver.get_cookies()
        #  Will get cookie Information written to cookies.json In file 
        with open("cookies.json", "w") as f:
            json.dump(cookies, f)

2、 from json File read cookie Information , Realize the automatic login of web pages

    def test_login(self):
        #  First open the page you want to log in to ( Not logged in )
        self.driver.get("https://work.weixin.qq.com/")
        #  Read cookie Information 
        with open("cookies.json", "r") as f:
            cookies = json.load(f)
        #  adopt add_cookie Methods to inject cookie,add_cookie Method to open the web page to be injected before injection , Is the first line of code 
        for cookie in cookies:
            self.driver.add_cookie(cookie)
        #  After injection , Reopen the login page 
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        self.driver.find_element_by_id("menu_contacts").click()
        sleep(3)

 

 

原网站

版权声明
本文为[gchh_ spring]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211321068824.html