远程WebDriver
Selenium lets you automate browsers on remote computers if there is a Selenium Grid running on them. The computer that executes the code is referred to as the client computer, and the computer with the browser and driver is referred to as the remote computer or sometimes as an end-node. To direct Selenium tests to the remote computer, you need to use a Remote WebDriver class and pass the URL including the port of the grid on that machine. Please see the grid documentation for all the various ways the grid can be configured.
Basic Example
The driver needs to know where to send commands to and which browser to start on the Remote computer. So an address and an options instance are both required.
ChromeOptions options = new ChromeOptions();
driver = new RemoteWebDriver(gridUrl, options);
options = webdriver.ChromeOptions()
driver = webdriver.Remote(command_executor=server, options=options)
var options = new ChromeOptions();
driver = new RemoteWebDriver(GridUrl, options);
options = Selenium::WebDriver::Options.chrome
driver = Selenium::WebDriver.for :remote, url: grid_url, options: options
Uploads
Uploading a file is more complicated for Remote WebDriver sessions because the file you want to upload is likely on the computer executing the code, but the driver on the remote computer is looking for the provided path on its local file system. The solution is to use a Local File Detector. When one is set, Selenium will bundle the file, and send it to the remote machine, so the driver can see the reference to it. Some bindings include a basic local file detector by default, and all of them allow for a custom file detector.
((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
WebElement fileInput = driver.findElement(By.cssSelector("input[type=file]"));
fileInput.sendKeys(uploadFile.getAbsolutePath());
driver.findElement(By.id("file-submit")).click();
Python adds a local file detector to remote webdriver instances by default, but you can also create your own class.
driver.file_detector = LocalFileDetector()
file_input = driver.find_element(By.CSS_SELECTOR, "input[type='file']")
file_input.send_keys(upload_file)
driver.find_element(By.ID, "file-submit").click()
((RemoteWebDriver)driver).FileDetector = new LocalFileDetector();
IWebElement fileInput = driver.FindElement(By.CssSelector("input[type=file]"));
fileInput.SendKeys(uploadFile);
driver.FindElement(By.Id("file-submit")).Click();
driver.file_detector = ->((filename, *)) { filename.include?('selenium') && filename }
file_input = driver.find_element(css: 'input[type=file]')
file_input.send_keys(upload_file)
driver.find_element(id: 'file-submit').click
Downloads
Chrome, Edge and Firefox each allow you to set the location of the download directory. When you do this on a remote computer, though, the location is on the remote computer’s local file system. Selenium allows you to enable downloads to get these files onto the client computer.
Enable Downloads in the Grid
Regardless of the client, when starting the grid in node or standalone mode, you must add the flag:
--enable-managed-downloads true
Enable Downloads in the Client
The grid uses the se:downloadsEnabled
capability to toggle whether to be responsible for managing the browser location.
Each of the bindings have a method in the options class to set this.
ChromeOptions options = new ChromeOptions();
options.setEnableDownloads(true);
driver = new RemoteWebDriver(gridUrl, options);
options = webdriver.ChromeOptions()
options.enable_downloads = True
driver = webdriver.Remote(command_executor=server, options=options)
ChromeOptions options = new ChromeOptions
{
EnableDownloads = true
};
driver = new RemoteWebDriver(GridUrl, options);
options = Selenium::WebDriver::Options.chrome(enable_downloads: true)
driver = Selenium::WebDriver.for :remote, url: grid_url, options: options
List Downloadable Files
Be aware that Selenium is not waiting for files to finish downloading, so the list is an immediate snapshot of what file names are currently in the directory for the given session.
List<String> files = ((HasDownloads) driver).getDownloadableFiles();
files = driver.get_downloadable_files()
IReadOnlyList<string> names = ((RemoteWebDriver)driver).GetDownloadableFiles();
files = driver.downloadable_files
Download a File
Selenium looks for the name of the provided file in the list and downloads it to the provided target directory.
((HasDownloads) driver).downloadFile(downloadableFile, targetDirectory);
driver.download_file(downloadable_file, target_directory)
((RemoteWebDriver)driver).DownloadFile(downloadableFile, targetDirectory);
driver.download_file(downloadable_file, target_directory)
Delete Downloaded Files
By default, the download directory is deleted at the end of the applicable session, but you can also delete all files during the session.
((HasDownloads) driver).deleteDownloadableFiles();
driver.delete_downloadable_files()
((RemoteWebDriver)driver).DeleteDownloadableFiles();
driver.delete_downloadable_files
Browser specific functionalities
Each browser has implemented special functionality that is available only to that browser. Each of the Selenium bindings has implemented a different way to use those features in a Remote Session
Java requires you to use the Augmenter class, which allows it to automatically pull in implementations for all interfaces that match the capabilities used with the RemoteWebDriver
driver = new Augmenter().augment(driver);
Of interest, using the RemoteWebDriverBuilder
automatically augments the driver, so it is a great way
to get all the functionality by default:
RemoteWebDriver.builder()
.address(gridUrl)
.oneOf(new ChromeOptions())
.setCapability("ext:options", Map.of("key", "value"))
.config(ClientConfig.defaultConfig())
.build();
var customCommandDriver = driver as ICustomDriverCommandExecutor;
customCommandDriver.RegisterCustomDriverCommands(FirefoxDriver.CustomCommandDefinitions);
var screenshotResponse = customCommandDriver
.ExecuteCustomDriverCommand(FirefoxDriver.GetFullPageScreenshotCommand, null);
追踪客户端请求
此功能仅适用于Java客户端绑定 (Beta版以后). 远程WebDriver客户端向Selenium网格服务器发送请求, 后者将请求传递给WebDriver. 应该在服务器端和客户端启用跟踪, 以便端到端地追踪HTTP请求. 两端都应该有一个指向可视化框架的追踪导出器设置. 默认情况下, 对客户端和服务器都启用追踪. 若设置可视化框架Jaeger UI及Selenium Grid 4, 请参阅所需版本的追踪设置 .
对于客户端设置, 请执行以下步骤.
添加所需依赖
可以使用Maven安装追踪导出器的外部库. 在项目pom.xml中添加 opentelemetry-exporter-jaeger 和 grpc-netty 的依赖项:
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-jaeger</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.35.0</version>
</dependency>
在运行客户端时添加/传递所需的系统属性
System.setProperty("otel.traces.exporter", "jaeger");
System.setProperty("otel.exporter.jaeger.endpoint", "http://localhost:14250");
System.setProperty("otel.resource.attributes", "service.name=selenium-java-client");
ImmutableCapabilities capabilities = new ImmutableCapabilities("browserName", "chrome");
WebDriver driver = new RemoteWebDriver(new URL("http://www.example.com"), capabilities);
driver.get("http://www.google.com");
driver.quit();
有关所需Selenium版本 及其外部依赖关系版本等更多信息, 请参阅追踪设置 .
更多信息请访问: