测试人社区

Selenium 在自动化操作业务网址,发现无法进入

老师好,我照着baidu的做成了,但是换成其他的网址就是会不成功,看到跳出来的data页面了,但是就是不会继续了,pycharm 报错说是元素找错了,但是我实际在console里面就是ok的呢,我认为应该是没有进入网页,所以没有找到元素,求老师给看看

image
1.考虑过是不是由于时间的原因导致的,于是加长了等待时间,但是发现即使加了等待时间,还是会出现打不开网页的场景,手动自己打开网页是ok的
2.自己把后面的元素去掉了,只上到网站,发现在进入网页遇到问题了,试了baidu 是ok的
3.网址是只有公司内网才能进入,不知道这个是否会存在影响

查找到元素
image

markdown文件如下

‘’’
F
test_genesis.py:14 (TestGenesisLogin.test_wait)
self = <test_genesis.TestGenesisLogin object at 0x118dfcba8>

def test_wait(self):
    sleep(5)
  self.driver.find_element(By.XPATH, '//*[@id="username"]').send_keys("yuanjiejiayou")

test_genesis.py:17:


/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py:978: in find_element
‘value’: value})[‘value’]
/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py:321: in execute

那就是说页面没打开?网址是正常请求了但是打开很慢还是说就停在了data页面没有接着去访问你写的这个地址了?自己手动打开是OK的 是用了多久页面加载完毕的呢?
你说加长了等待时间,那么你是怎么加长的呢?用sleep还是增加了隐式等待的时间?
会出现网页打不开的场景 是说偶尔网页会打不开,还是说每次网页都打不开?那么你这个元素没找到的报错是网页打开了没找到元素报的错吗?网页打不开的时候报错信息是什么呢?

  • 是的访问了我的地址,手动打开是8s
  • image
  • 我用的隐式等待,self.driver.implicitly_wait(20)
  • 是每次网页都打不开,报错信息是网页打不开

你手工也无法访问这个网页吗?

手动可以的,上图是selenium跑出来的结果

你这个格式有点乱啊。。能不能用markdown格式把报错信息和代码格式化一下,不然这一段一段的挺奇怪的看着

手动打开要8秒钟 那首先你把你的隐式等待设置到10秒 然后 在你的find_element语句前面加一句

time.sleep(5)

再试试看能不能定位到元素和打开页面
另外不要随便删除链接中的信息,就用原本的链接访问就好

好的,老师,我更新了,在原始帖子内容里面 http://note.youdao.com/noteshare?id=99c789a9fa9621a0dc422838f3a0852f

额 论坛就支持markdown格式 不是说你把日志放在哪 而是你的日志看起来格式特别乱 隔几行有一个代码格式又有普通的文本格式,看起来特别费劲 我建议你看一下markdown格式 用一对```包裹你的日志信息,让格式统一一下 或者是你复制到txt里用附件放到帖子上也行

老师,我没有上传的权限

‘’’
F
test_genesis.py:14 (TestGenesisLogin.test_wait)
self = <test_genesis.TestGenesisLogin object at 0x115372be0>

def test_wait(self):
    sleep(5)
  self.driver.find_element(By.XPATH, '//*[@id="username"]').send_keys("yuanjiejiayou")

test_genesis.py:17:


/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py:978: in find_element
‘value’: value})[‘value’]
/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py:321: in execute
self.error_handler.check_response(response)


self = <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x113fa8eb8>
response = {‘status’: 404, ‘value’: ‘{“value”:{“error”:“no such element”,“message”:“no such element: Unable to locate element: {…7fff7c6eb249 _pthread_start + 66\n20 libsystem_pthread.dylib 0x00007fff7c6e740d thread_start + 13\n”}}’}

def check_response(self, response):
    """
        Checks that a JSON response from the WebDriver does not have an error.

        :Args:
         - response - The JSON response from the WebDriver server as a dictionary
           object.

        :Raises: If the response contains an error message.
        """
    status = response.get('status', None)
    if status is None or status == ErrorCode.SUCCESS:
        return
    value = None
    message = response.get("message", "")
    screen = response.get("screen", "")
    stacktrace = None
    if isinstance(status, int):
        value_json = response.get('value', None)
        if value_json and isinstance(value_json, basestring):
            import json
            try:
                value = json.loads(value_json)
                if len(value.keys()) == 1:
                    value = value['value']
                status = value.get('error', None)
                if status is None:
                    status = value["status"]
                    message = value["value"]
                    if not isinstance(message, basestring):
                        value = message
                        message = message.get('message')
                else:
                    message = value.get('message', None)
            except ValueError:
                pass

    exception_class = ErrorInResponseException
    if status in ErrorCode.NO_SUCH_ELEMENT:
        exception_class = NoSuchElementException
    elif status in ErrorCode.NO_SUCH_FRAME:
        exception_class = NoSuchFrameException
    elif status in ErrorCode.NO_SUCH_WINDOW:
        exception_class = NoSuchWindowException
    elif status in ErrorCode.STALE_ELEMENT_REFERENCE:
        exception_class = StaleElementReferenceException
    elif status in ErrorCode.ELEMENT_NOT_VISIBLE:
        exception_class = ElementNotVisibleException
    elif status in ErrorCode.INVALID_ELEMENT_STATE:
        exception_class = InvalidElementStateException
    elif status in ErrorCode.INVALID_SELECTOR \
            or status in ErrorCode.INVALID_XPATH_SELECTOR \
            or status in ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPER:
        exception_class = InvalidSelectorException
    elif status in ErrorCode.ELEMENT_IS_NOT_SELECTABLE:
        exception_class = ElementNotSelectableException
    elif status in ErrorCode.ELEMENT_NOT_INTERACTABLE:
        exception_class = ElementNotInteractableException
    elif status in ErrorCode.INVALID_COOKIE_DOMAIN:
        exception_class = InvalidCookieDomainException
    elif status in ErrorCode.UNABLE_TO_SET_COOKIE:
        exception_class = UnableToSetCookieException
    elif status in ErrorCode.TIMEOUT:
        exception_class = TimeoutException
    elif status in ErrorCode.SCRIPT_TIMEOUT:
        exception_class = TimeoutException
    elif status in ErrorCode.UNKNOWN_ERROR:
        exception_class = WebDriverException
    elif status in ErrorCode.UNEXPECTED_ALERT_OPEN:
        exception_class = UnexpectedAlertPresentException
    elif status in ErrorCode.NO_ALERT_OPEN:
        exception_class = NoAlertPresentException
    elif status in ErrorCode.IME_NOT_AVAILABLE:
        exception_class = ImeNotAvailableException
    elif status in ErrorCode.IME_ENGINE_ACTIVATION_FAILED:
        exception_class = ImeActivationFailedException
    elif status in ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS:
        exception_class = MoveTargetOutOfBoundsException
    elif status in ErrorCode.JAVASCRIPT_ERROR:
        exception_class = JavascriptException
    elif status in ErrorCode.SESSION_NOT_CREATED:
        exception_class = SessionNotCreatedException
    elif status in ErrorCode.INVALID_ARGUMENT:
        exception_class = InvalidArgumentException
    elif status in ErrorCode.NO_SUCH_COOKIE:
        exception_class = NoSuchCookieException
    elif status in ErrorCode.UNABLE_TO_CAPTURE_SCREEN:
        exception_class = ScreenshotException
    elif status in ErrorCode.ELEMENT_CLICK_INTERCEPTED:
        exception_class = ElementClickInterceptedException
    elif status in ErrorCode.INSECURE_CERTIFICATE:
        exception_class = InsecureCertificateException
    elif status in ErrorCode.INVALID_COORDINATES:
        exception_class = InvalidCoordinatesException
    elif status in ErrorCode.INVALID_SESSION_ID:
        exception_class = InvalidSessionIdException
    elif status in ErrorCode.UNKNOWN_METHOD:
        exception_class = UnknownMethodException
    else:
        exception_class = WebDriverException
    if value == '' or value is None:
        value = response['value']
    if isinstance(value, basestring):
        if exception_class == ErrorInResponseException:
            raise exception_class(response, value)
        raise exception_class(value)
    if message == "" and 'message' in value:
        message = value['message']

    screen = None
    if 'screen' in value:
        screen = value['screen']

    stacktrace = None
    if 'stackTrace' in value and value['stackTrace']:
        stacktrace = []
        try:
            for frame in value['stackTrace']:
                line = self._value_or_default(frame, 'lineNumber', '')
                file = self._value_or_default(frame, 'fileName', '<anonymous>')
                if line:
                    file = "%s:%s" % (file, line)
                meth = self._value_or_default(frame, 'methodName', '<anonymous>')
                if 'className' in frame:
                    meth = "%s.%s" % (frame['className'], meth)
                msg = "    at %s (%s)"
                msg = msg % (meth, file)
                stacktrace.append(msg)
        except TypeError:
            pass
    if exception_class == ErrorInResponseException:
        raise exception_class(response, message)
    elif exception_class == UnexpectedAlertPresentException:
        alert_text = None
        if 'data' in value:
            alert_text = value['data'].get('text')
        elif 'alert' in value:
            alert_text = value['alert'].get('text')
        raise exception_class(message, screen, stacktrace, alert_text)
  raise exception_class(message, screen, stacktrace)

E selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:“xpath”,“selector”:"//*[@id=“username”]"}
E (Session info: chrome=81.0.4044.138)

/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py:242: NoSuchElementException

‘’’

测试上传.txt (6 字节) 算了别纠结格式了 ,我前面回复了方法你先试试看 后续还有什么报错在回复吧
另:我试了一下 txt上传应该没问题啊

老师,我重启电脑就好了