I have seen almost everybody using the statement WebDriver driver=new FirefoxDriver(); here we have created an instance of FirefoxDriver class having type as Webdriver;
What if i directly create an instance of FirefoxDriver as FirefoxDriver driver = new FirefoxDriver();
I have gone through many discussion where it is said that Webdriver is an interface. I know what interface is and how it works. I want to know if FirefoxDriver driver = new FirefoxDriver(); is correct when i know that Firefox is the only browser i am gonna use in my selenium script?
As you mentioned in your question that know that Firefox is the only browser you are gonna use in your selenium script then there is no constraint in using the following line of code :
FirefoxDriver driver = new FirefoxDriver();
Here you can find a detailed discussion on What is the difference between ChromeDriver and WebDriver in selenium?
Related
I am setting up a chrome driver with the help of Selenium and Java. I want this driver to be executed headless but I can't find out the way. Can you explain to me what do I need to do?
My code sample:
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICITY_TIME, TimeUnit.SECONDS);
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH); // OS and Browser options
ChromeOptions options = new ChromeOptions(); // create options instance
options.addArguments("--headless"); // add an option
driver = new ChromeDriver(options); // create a driver with the specific options instance
You just need to create a ChromeOptions object in which you need to save the options for your own driver.
To add your own options just use this: options.addArguments(); and in the parenthesis insert your option in string mode.
For more details and documentation please also check here:
http://chromedriver.chromium.org/capabilities
I think this is going to work.
I want to use Selenium WebDriver to compile html String, the only examples i found are for opening files like :
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/jgong/Desktop/a.html");
I need:
WebDriver driver = new FirefoxDriver();
driver.get(htmlString);
and then use the getPageSource() method, is it possible?
Thanks.
You can use the data URI scheme to load some HTML with Selenium:
WebDriver driver = new FirefoxDriver();
driver.get("data:text/html;charset=utf-8," + htmlString);
I'm stuck on how can I open the instance of webdriver on my html page using java.
WebDriver driver = new ChromeDriver(); // Open the Application
the code below open the instance of driver chrome.
If you want to start chrome browser, first download chromeDriver from here After that we need to specify this downloaded chromedriver and then start the driver instance
System.setProperty("webdriver.chrome.driver", "\\downloaded driver path");
WebDriver driver = new ChromeDriver();
driver.get("http://URL here/");
Then as said need to switch to frame, so
driver.switchTo().frame("iframe id or name");
//or
driver.switchTo().frame("index like 0, 1");
//or
driver.switchTo().frame(driver.findElement(By.xpath("//iframe"))); //iframe location
//after work in frame is completed, then we need to switch back to default content
driver.switchTo().defaultContent();
Thank You,
Murali
I am working on Selenium WebDriver automation in java programming language. In my test suite that initiates the browser window once and perform all the tests. I want to clear the browser cache before running some tests without restarting the browser. Is there any command/function, that can achieve the purpose? Thanks.
This is what I use in Python:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('chrome://settings/clearBrowserData')
driver.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER)
You can try converting these into Java. Hope this will help! :)
At least in Chrome, I strongly believe that if you go incognito you wont to have to clean up your cookies. You can set your options like following (the :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def _options():
options = Options()
options.add_argument('--ignore-certificate-errors')
#options.add_argument("--test-type")
options.add_argument("--headless")
options.add_argument("--incognito")
options.add_argument('--disable-gpu') if os.name == 'nt' else None # Windows workaround
options.add_argument("--verbose")
return options
and call like this:
with webdriver.Chrome(options=options) as driver:
driver.implicitly_wait(conf["implicitly_wait"])
driver.get(conf["url"])
For IE
DesiredCapabilities ieCap = DesiredCapabilities.internetExplorer();
ieCap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
For Chrome:
https://code.google.com/p/chromedriver/issues/detail?id=583
To delete cookies:
driver.manage().deleteAllCookies();
The following code is based on #An Khang 's answers. and it is working properly on Chrome 78.
ChromeDriver chromeDriver = new ChromeDriver();
chromeDriver.manage().deleteAllCookies();
chromeDriver.get("chrome://settings/clearBrowserData");
chromeDriver.findElementByXPath("//settings-ui").sendKeys(Keys.ENTER);
return chromeDriver;
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.get("chrome://settings/clearBrowserData");
driver.findElement(By.xpath("//settings-ui")).sendKeys(Keys.ENTER);
On Google chrome you can use this script:
driver.get("chrome://settings/clearBrowserData");
JavascriptExecutor jse = (JavascriptExecutor)driver;
WebElement clearData = (WebElement) jse.executeScript("return document.querySelector(\"body > settings-ui\").shadowRoot.querySelector(\"#main\").shadowRoot.querySelector(\"settings-basic-page\").shadowRoot.querySelector(\"#basicPage > settings-section:nth-child(8) > settings-privacy-page\").shadowRoot.querySelector(\"settings-clear-browsing-data-dialog\").shadowRoot.querySelector(\"#clearBrowsingDataConfirm\")");
((JavascriptExecutor)driver).executeScript("arguments[0].click();", clearData);
import org.openqa.selenium.Keys;
you need to import the Keys in newer version and change the last line to findElement by xpath
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.get("chrome://settings/clearBrowserData");
driver.findElement(By.xpath("//settings-ui")).sendKeys(Keys.ENTER);
How to use this method? What parameter should I pass to "capabilityName" and "value"?
public void setCapability(java.lang.String capabilityName,java.lang.String value);
For Chrome specifically, you can find the capabilities here: https://sites.google.com/a/chromium.org/chromedriver/capabilities-aka-chromeoptions
There is a more general overview of capabilities on the selenium wiki: http://code.google.com/p/selenium/wiki/DesiredCapabilities
Here is an example that maximizes the browser window (for chrome). Enable/Disable the second while running it and see the difference.
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");