[英]How to send HTTP Request Code using Selenium WebDriver with Java?
I have written tests using Selenium WebDriver and I have problems with authentication to the website. 我使用Selenium WebDriver编写了测试,但我对网站的身份验证存在问题。 For Chrome and Firefox I've used a basic authentication from URL:
对于Chrome和Firefox,我使用了URL中的基本身份验证:
WebDriver.get("https://u:p@example.com/");
And it works fine, it bypasses the authentication popup and my test proceeds to the website. 它工作正常,绕过身份验证弹出窗口,我的测试进入网站。
In Explorer and Edge I cannot use this feature as it is disabled. 在Explorer和Edge中,我无法使用此功能,因为它已被禁用。 I tried to use Robot and it works for Explorer, but in Edge the test is stuck on the authentication window, it does not send any keys to the username or password fields.
我尝试使用Robot,它适用于资源管理器,但在Edge中,测试停留在身份验证窗口,它不会向用户名或密码字段发送任何密钥。
WebDriver.get("https://example.com/");
Robot robot = new Robot();
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_U);
Thread.sleep(100);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(100);
robot.keyPress(KeyEvent.VK_P);
Thread.sleep(100);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(100);
robot.keyPress(KeyEvent.VK_ENTER);
Right now I've tried a http request: 现在我尝试了一个http请求:
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("https://example.com/");
httpGet.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("u", "p"), "UTF-8", false));
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity responseEntity = httpResponse.getEntity();
System.out.println(httpResponse);
WebDriver.get("https://example.com/");
And I get this response: 我得到了这样的答复:
HTTP/1.1 200 OK for successful login
HTTP / 1.1 200确定成功登录
But when the webdriver goes to page I still get an authentication request. 但是当webdriver转到页面时,我仍然会收到身份验证请求。
How can I bypass authentication window and login to the website in Edge and Explorer, any thoughts? 如何绕过身份验证窗口并登录Edge和资源管理器中的网站,有什么想法吗?
This worked in IE for me for dealing with a login popup. 这在IE中用于处理登录弹出窗口。 Give it a shot.
试一试。 It basically waits for the login box to appear, and then attempts to login using that box.
它基本上等待登录框出现,然后尝试使用该框登录。 Fairly simple.
相当简单。
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert login = wait.until(ExpectedConditions.alertIsPresent());
login.authenticateUsing(new UserAndPassword("user","pass"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.