Web驱动器Selenium异常错误"服务器未找到"或者那样

时间:2018-06-07 09:46:45

标签: selenium selenium-webdriver webdriver

我通过python创建自动化应用程序。它应该注册Instagram。 我不是python中的专业人士。我需要一种机制来控制网页错误。在我的情况下,有时网页没有加载,它说'#34;服务器找不到"或者当连接不稳定页面没有加载dns问题和自动化失败&出现错误。 据我所知,我必须使用Try {} catch {}。但我不知道应该如何以及使用什么语法。 这是什么来源和例子?我无法找到。 Python2.7

Charamander

其中一个错误:

from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.keys import Keys
import names
import time
import random
import string
import bs4
import re
#import outlook
import urllib2
from createalias import create_alias
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium import webdriver
import selenium.common.exceptions


def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
        return ''.join(random.choice(chars) for _ in range(size))


def checkname():
        line = names.get_full_name(gender='male')
        line=line.replace(" ","")
        random.seed()
        num=random.randint(50,200)
        line=line+str(num)
        driver.get("http://www.instagram.com/"+line)
        try:
             if "Page Not Found" in driver.title:
                 return line
        except WebDriverException:
                return 0

profile = FirefoxProfile()
profile.set_preference('browser.cache.disk.enable', False)
profile.set_preference('browser.cache.memory.enable', False)
profile.set_preference('browser.cache.offline.enable', False)
profile.set_preference('network.cookie.cookieBehavior', 2)
driver = webdriver.Firefox(firefox_profile=profile)

line=""
#f=open(r'C:\Users\11\Desktop\emailes.txt','r')
for x in range(5, 10):
        username=checkname()
        if username !=0:
                        password=id_generator()
                        #email=f.readline().strip()
                        userdomain="abobakbaghdadi"+str(x)+"@gmail.com"
                        f2=open(r'C:\Users\11\Desktop\info.txt','a')
                        f2.write(str(userdomain) + "," + str(username) + "," + str(password))
                        f2.write("\n")
                        f2.close()
                        driver.get("http://www.instagram.com")
                        assert "Instagram" in driver.title
                        time.sleep(4)
                        elem=driver.find_element_by_name("emailOrPhone")
                        elem.clear
                        time.sleep(3)
                        elem.send_keys(userdomain)
                        elem=driver.find_element_by_name("fullName")
                        elem.clear
                        time.sleep(2)
                        elem.send_keys(username)
                        elem=driver.find_element_by_name("username")
                        elem.clear
                        elem.send_keys(username)
                        elem=driver.find_element_by_name("password")
                        elem.clear
                        elem.send_keys(password)
                        elem.send_keys(Keys.RETURN)
                        #flag=1
        else:
                x=x-1

1 个答案:

答案 0 :(得分:1)

python中的try / catch块是:

try:
    connect()
except Exception:
    print('Error while connecting')