selenium.common.exceptions.NoSuchElementException:消息:无法找到元素

时间:2019-02-25 05:12:02

标签: python python-3.x selenium testing

我遇到此错误:

  static HttpWebResponse SetRequestAndGetResponse(HttpWebRequest request, string payload = null)
        {
            while (true)
            {
                //To authorize the operation call, you need an access token which is part of the Authorization header
                request.Headers.Add("Authorization", AccessToken().Result.CreateAuthorizationHeader());
                //Set to false to be able to intercept redirects
                request.AllowAutoRedirect = false;

                if (!string.IsNullOrEmpty(payload))
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(payload);
                    request.ContentLength = byteArray.Length;
                    request.ContentType = "application/json";
                    //Write JSON byte[] into a Stream
                    request.GetRequestStream().Write(byteArray, 0, byteArray.Length);
                }
                else
                {
                    request.ContentLength = 0;
                }

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;

                // Requests to **Azure Data Catalog (ADC)** may return an HTTP 302 response to indicate
                // redirection to a different endpoint. In response to a 302, the caller must re-issue
                // the request to the URL specified by the Location response header. 
                if (response.StatusCode == HttpStatusCode.Redirect)
                {
                    string redirectedUrl = response.Headers["Location"];
                    HttpWebRequest nextRequest = WebRequest.Create(redirectedUrl) as HttpWebRequest;
                    nextRequest.Method = request.Method;
                    request = nextRequest;
                }
                else
                {
                    return response;
                }
            }
        }

会话文件的代码:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="username"]

我的测试代码:

def login(self, username, password):
    wb = self.app.wb
    self.app.open_home_page()
    wb.find_element_by_name("username").click()
    wb.find_element_by_name("username").clear()
    wb.find_element_by_name("username").send_keys(username)
    wb.find_element_by_xpath("//*[@id='login-form']/fieldset/input[2]").click()
    wb.find_element_by_name("password").click()
    wb.find_element_by_name("password").clear()
    wb.find_element_by_name("password").send_keys(password)
    wb.find_element_by_xpath("//*[@id='login-form']/fieldset/input[3]").click()


def is_logged_in_as(self, username):
    wb = self.app.wb
    return self.get_logged_user() == username

def get_logged_user(self):
    wb = self.app.wb
    return wb.find_element_by_xpath("//*[@id='breadcrumbs']/ul/li/a").text

可能是什么问题?

0 个答案:

没有答案