简体   繁体   中英

How to replace address with selenium webdriver module in python without close then open browser?

I have a for loop in my source code, This loop include codes that test URLs in a List; of course for the test it needs open browser but doing this work is enough just for 1 time, in second time the robot should just replace address...

This is my source:

    elif len(sys.argv) == 3 and sys.argv[2] == '--smart':
        telerikbugtag = "Telerik.Web.UI.WebResource.axd?type=rau"
        telerikuiVul = '{ "message" : "RadAsyncUpload handler is registered succesfully, however, it may not be accessed directly." }'
        fopenAddr = open("clean url.txt", "r")
        addrlist = fopenAddr.read().split('\n')
        addrlistcln = list(filter(str.strip, addrlist))
        fopenAddr.close()

        for i in tqdm(addrlistcln):
            # Hey Mahdi!!!! I think you see that you did wrong now! Be A God ! AllRight?! ;)
            try:
                try:
                    browser = webdriver.Firefox()
                    req = requests.get(i)
                    if req.status_code == 200 or req.status_code == 404:
                        browser.get(i)
                        time.sleep(4)
                        RedURL = browser.current_url
                        browser.close()

                        splitADR = RedURL.split('/')

                        filteredADR = list(filter(str.strip, splitADR))

                        if len(filteredADR) > 3:
                            reqadd = str(RedURL.replace(
                                str(RedURL).split('/').pop(), ""))
                        elif len(filteredADR) == 3:
                            reqadd = filteredADR[0] + '//' + \
                                filteredADR[1] + '/' + filteredADR[2] + '/'
                        else:
                            reqadd = filteredADR[0] + '//' + \
                                filteredADR[1] + '/'
                        reqHome = RedURL.split('/')
                        requthat = requests.get(reqadd + telerikbugtag)
                        regSoup = BeautifulSoup(
                            requthat.content, 'html.parser')
                        browser.close()

                        secondtest = requests.get(
                            reqHome[0] + "//" + reqHome[2] + '/' + telerikbugtag)
                        soupsec = BeautifulSoup(
                            secondtest.content, 'html.parser')

                        if telerikuiVul in regSoup and telerikuiVul not in soupsec:
                            fopenvul = open("vul list.txt", "a+")
                            fopenvul.write(reqadd + telerikbugtag + "\n")
                            fopenvul.close()
                        else:
                            print('\n' + i + ' NOT VUL :( ')

                    else:
                        print("Site Unavailable!")
                except Exception as er:
                    print('\n error... \n')
            except Exception as e:
                print("Site Error!")

    else:
        if(sys.argv[1] != '-h'):
            print("use '<Python3 source.py -h>' command")
        else:
            print('You are see Usage of This Script!')


help me to fix this.

You can just move the browser creation and closing out of the for loop

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

for uri in ("http://www.python.org", "http://www.google.es", "http://www.google.com"):
    driver.get(uri)
    assert "Python" in driver.title or "Google" in driver.title
    assert "No results found." not in driver.page_source

driver.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM