using OpenQA.Selenium; using OpenQA.Selenium.Firefox; namespace StackOverflowTests { class OpenNewTab { static void Main(string[] args) { IWebDriver driver = new FirefoxDriver(); driver.Navigate().GoToUrl("http://stackoverflow.com/"); IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + 't'); driver.Quit(); } } }
File pathToBinary = new File("C:\\user\\Programme\\FirefoxPortable\\App\\Firefox\\firefox.exe"); FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary); FirefoxProfile firefoxProfile = new FirefoxProfile(); WebDriver driver = new FirefoxDriver(ffBinary,firefoxProfile);
private WebDriver createLegacyFirefoxDriver(Config config, Proxy proxy) { FirefoxOptions firefoxOptions = createFirefoxOptions(config, proxy); firefoxOptions.setLegacy(true); return new FirefoxDriver(firefoxOptions); } }
@BeforeEach public void setup(@TempDirectory.TempDir Path folder) throws IOException { Assumptions.assumeTrue(Boolean.getBoolean("smoke.test"), "Smoke tests disabled (set System property 'smoke.test')"); this.downloadDir = folder.toFile(); FirefoxProfile fxProfile = new FirefoxProfile(); fxProfile.setPreference("browser.download.folderList", 2); fxProfile.setPreference("browser.download.manager.showWhenStarting", false); fxProfile.setPreference("browser.download.dir", this.downloadDir.getAbsolutePath()); fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip,application/x-compress,application/octet-stream"); FirefoxOptions options = new FirefoxOptions().setProfile(fxProfile); this.driver = new FirefoxDriver(options); ((JavascriptExecutor) this.driver).executeScript("window.focus();"); Actions actions = new Actions(this.driver); this.enterAction = actions.sendKeys(Keys.ENTER).build(); }
FirefoxProfile p = new FirefoxProfile(); p.setPreference("javascript.enabled", false); driver = new FirefoxDriver(p);
FirefoxDriver driver = new FirefoxDriver(); driver.get("http://localhost:8080/page"); File file = null; try { file = new File(YourClass.class.getClassLoader().getResource("file.txt").toURI()); } catch (URISyntaxException e) { e.printStackTrace(); } Assert.assertTrue(file.exists()); WebElement browseButton = driver.findElement(By.id("myfile")); browseButton.sendKeys(file.getAbsolutePath());
FirefoxOptions createFirefoxOptions(Config config, Proxy proxy) { FirefoxOptions firefoxOptions = new FirefoxOptions(); firefoxOptions.setHeadless(config.headless()); if (!config.browserBinary().isEmpty()) { log.info("Using browser binary: " + config.browserBinary()); firefoxOptions.setBinary(config.browserBinary()); } firefoxOptions.addPreference("network.automatic-ntlm-auth.trusted-uris", "http://,https://"); firefoxOptions.addPreference("network.automatic-ntlm-auth.allow-non-fqdn", true); firefoxOptions.addPreference("network.negotiate-auth.delegation-uris", "http://,https://"); firefoxOptions.addPreference("network.negotiate-auth.trusted-uris", "http://,https://"); firefoxOptions.addPreference("network.http.phishy-userpass-length", 255); firefoxOptions.addPreference("security.csp.enable", false); firefoxOptions.addPreference("network.proxy.no_proxies_on", ""); firefoxOptions.merge(createCommonCapabilities(config, proxy)); firefoxOptions = transferFirefoxProfileFromSystemProperties(firefoxOptions); return firefoxOptions; }
final String firebugPath = "C:\\FF_Profile\\firebug.xpi"; FirefoxProfile profile = new FirefoxProfile(); profile.addExtension(new File(firebugPath)); // Add more if needed WebDriver driver = new FirefoxDriver(profile);
private FirefoxOptions transferFirefoxProfileFromSystemProperties(FirefoxOptions currentFirefoxOptions) { String prefix = "firefoxprofile."; FirefoxProfile profile = Optional.ofNullable(currentFirefoxOptions.getProfile()) .orElseGet(FirefoxProfile::new); for (String key : System.getProperties().stringPropertyNames()) { if (key.startsWith(prefix)) { String capability = key.substring(prefix.length()); String value = System.getProperties().getProperty(key); log.config("Use " + key + "=" + value); if (value.equals("true") || value.equals("false")) { profile.setPreference(capability, Boolean.valueOf(value)); } else if (value.matches("^-?\\d+$")) { //if integer profile.setPreference(capability, Integer.parseInt(value)); } else { profile.setPreference(capability, value); } } } return currentFirefoxOptions.setProfile(profile); } }
FirefoxProfile profile = new FirefoxProfile(new File("D:\\Selenium Profile")); WebDriver driver = new FirefoxDriver(profile);
private Capabilities getHeadlessCapabilities(Config config, Browser browser) { log.info("Starting in headless mode"); if (browser.isChrome()) { ChromeOptions options = new ChromeOptions(); options.setHeadless(config.headless()); return options; } else if (browser.isFirefox()) { FirefoxOptions options = new FirefoxOptions(); options.setHeadless(config.headless()); return options; } else { log.warning("Headless mode on remote server is only supported for Chrome/Firefox, setting will be ignored."); } return new DesiredCapabilities(); }
Capabilities getBrowserBinaryCapabilities(Config config, Browser browser) { log.info("Using browser binary: " + config.browserBinary()); if (browser.isChrome()) { ChromeOptions options = new ChromeOptions(); options.setBinary(config.browserBinary()); return options; } else if (browser.isFirefox()) { FirefoxOptions options = new FirefoxOptions(); options.setBinary(config.browserBinary()); return options; } else { log.warning("Changing browser binary on remote server is only supported for Chrome/Firefox, setting will be ignored."); } return new DesiredCapabilities(); }
System.setProperty("webdriver.firefox.profile", "MySeleniumProfile"); WebDriver driver = new FirefoxDriver(...);
FirefoxProfile firefoxProfile = new FirefoxProfile(); File extension = new File("extension.xpi"); firefoxProfile.addExtension(extension); DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile); driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
File pathToBinary = new File("C:\\Program Files\\Mozilla Firefox15\\Firefox.exe"); FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary); FirefoxProfile firefoxProfile = new FirefoxProfile(); FirefoxDriver _driver = new FirefoxDriver(ffBinary,firefoxProfile);
File profileDirectory = new File("c://mach//lib//prof"); FirefoxProfile profile = new FirefoxProfile(profileDirectory); DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability(FirefoxDriver.PROFILE, profile); driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
WebDriver driver; System.setProperty("webdriver.gecko.driver", "<Path to your WebDriver>"); driver =new FirefoxDriver();
WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.scrollBy(0,250)", "");
private WebDriver createFirefoxDriver(Config config, Proxy proxy) { FirefoxOptions options = createFirefoxOptions(config, proxy); return new FirefoxDriver(options); }
WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');"); driver.findElement(By.xpath("//input[@name='body']")).clear(); driver.findElement(By.xpath("//input[@name='body']")).sendKeys("Ripon: body text");