Selenium WebDriver - compile HTML string - java

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);

Related

Is it required to set the DISPLAY for headless firefox browser on linux machine

I want to run the headless browser and below is the code for same.
However when i ran it. it shows "Error: no DISPLAY environment variable specified"
try {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("webdriver.log.driver", "INFO");
profile.setPreference("webdriver.log.file", targetDir + File.separator + "firefoxSeleniumServer.log");
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv;text/plain");
if(platform.equalsIgnoreCase("linux")) {
DesiredCapabilities desiredCap = DesiredCapabilities.firefox();
profile.setPreference("browser.download.dir",System.getProperty("user.dir")+ File.separator + "target");
System.setProperty("webdriver.gecko.driver", "/test/geckodriver/geckodriver");
System.setProperty("webdriver.firefox.bin","/usr/bin/firefox/firefox");
desiredCap.setCapability("headless", true);
driver = new FirefoxDriver();
}
However when i set the display it shows unable to open firefox on DISPLAY:99
Also i tried setting the xvfb as well. But that also did not work.
As i am using gekco driver here, do i need to do some more configurations.
I think you are supposed to provide the DesiredCapabilities object as a parameter to the FirefoxDriver constructor:
References
The javadoc for FirefoxDriver.
What is the use of DesiredCapabilities in Selenium WebDriver?

Is this correct - FirefoxDriver driver = new FirefoxDriver();?

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?

Is there a way to display webdriver instance on iframe site

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

Clear cache before running some Selenium WebDriver tests using Java

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 Disable JavaScript in WebDriver when automating in Selenium?

I am currently trying to write a test that tests the non-JS version of my website. I use Selenium with Java. I have tried this:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(profile);
driver.get();
However this isn't working. It just loads the page with JavaScript Enabled.
As a workaround I did, this, for the requirement.
It will manually set the javascript.enabled property to false by the following script.
WebDriver driver = new FirefoxDriver();
driver.get("about:config");
Actions act = new Actions(driver);
act.sendKeys(Keys.RETURN).sendKeys("javascript.enabled").perform();
act.sendKeys(Keys.TAB).sendKeys(Keys.RETURN).perform();

Categories