
[英]Selenium Basic Authentication via URL HeadlessChrome ( On Linux Server)
[英]Selenium - Basic Authentication via url
在我的Selenium-Test
(使用chromedriver-2.24
)中,我尝试使用以下语句通过基本身份验证访问我的网页:
WebDriver driver = ...;
driver.get("http://admin:admin@localhost:8080/project/");
但是谷歌浏览器在控制台中给我以下警告:
[弃用] 其 URL 包含嵌入式凭据(例如
https://user:pass@host/
)的子资源请求被阻止。 有关详细信息,请参阅https://www.chromestatus.com/feature/5669008342777856 。
在标记的链接中提到支持被删除:
在子资源请求中删除对嵌入式凭据的支持。 (已删除)
我现在的问题是,还有其他方法可以从 Selenium 进行基本身份验证吗?
注意:这没有帮助: How to Handle HTTP Basic Auth headers in Selenium Webdriver using Java?
仅针对子资源阻止通过 url 进行的基本身份验证。 所以你仍然可以在域上使用它:
driver.get("http://admin:admin@localhost:8080");
driver.get("http://localhost:8080/project");
您还可以创建一个小的扩展来在请求时自动设置凭据:
options = webdriver.ChromeOptions()
options.add_extension(r'C:\dev\credentials.zip')
https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46
此link
有一些更新:
Chromium Issue 435547
资源请求中嵌入凭据的支持。 (已删除)我们应该阻止对包含嵌入式凭据的子资源的请求(例如“ http://ima_user:hunter2@example.com/yay.tiff ”)。 此类资源将作为网络错误处理。
但是,通过Selenium-Java绑定,基本身份验证功能仍然适用于Selenium 3.4.0 、 geckodriver v0.18.0 、 chromedriver v2.31.488763 、 Google Chrome 60.x和Mozilla Firefox 53.0 。
这是尝试使用一组有效凭据打开 URL http://the-internet.herokuapp.com/basic_auth的示例代码,它可以工作。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BasicAuthentication_FF
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://admin:admin@the-internet.herokuapp.com/basic_auth");
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class BasicAuthentication_Chrome
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.navigate().to("http://admin:admin@the-internet.herokuapp.com/basic_auth");
}
}
Florent B. 在 URL 上调用 .get 的方法两次对我有用,稍作修改。 在JS中:
driver
.get('http://admin:admin@localhost:8080')
.then( () => driver.get('http://localhost:8080') )
使用 ChromeDriver 2.33.506092 在 google chrome 62.0.3202.94 上工作,并且该方法似乎与带有 geckodriver 0.19.1 的 firefox 56.0.2 和 phantomjs 2.1.1 兼容,所有这些都在 Debian linux 9 下。
我相信正在发生的是第一个调用设置浏览器发送的授权标头。 第二个调用从 URL 中删除凭据,并且凭据不再应用于子资源。 then
同步两个请求以确保顺序。
chrome 的新功能和通过远程调试的基本身份验证:仅用于将其链接到此处,以便卡住的人可以找到 chrome 等的解决方案: seleniumgrid 中的 Chrome 远程调试
Selenium 4 支持基本认证
WebDriver driver = new ChromeDriver();
HasAuthentication authentication = (HasAuthentication) driver;
authentication.register(() -> new UsernameAndPassword("username", "pwd"));
driver.get("your-site.com");
https://www.selenium.dev/blog/2021/a-tour-of-4-authentication/
使用 selenium driver.get(URL) 方法在 JavaScript 弹出窗口中加载提示进行身份验证的 URL 将不直接支持这种基本身份验证,我也在这里卡了很长时间。 这是因为 Chrome 驱动程序在更新 59(可能)后将不允许使用此类身份验证技术。 仍然存在通过 Selenium 使用浏览器中的 JavaScript 引擎加载此类 URL 的后门。
driver.get("https://www.google.com");
JavascriptExecutor jse = (JavascriptExecutor) driver;
URL = "https://username:password@www.example.com";
jse.executeScript("window.open('"+URL+"')");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.