I'm getting screenshots of webpages using Chromedriver. My code works well.
I'm now attempting to remove the ugly scrollbars. Would it be possible to inject CSS into the page? I have seen a couple of similar questions, which do hint that this is possible ("You can also make Chromedriver open the url in a popup without scrollbars. You can do this using some Javascript.") but doesn't show how this is possible.
Has anybody got any idea as to how to do this?
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Size = new Size(1100, 1100);
driver.Navigate().GoToUrl("http://google.com/");
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
//Temp Img
string screenshot = ss.AsBase64EncodedString;
byte[] screenshotAsByteArray = ss.AsByteArray;
ss.SaveAsFile(fileNameTemp, ScreenshotImageFormat.Jpeg);
ss.ToString();
// Create a driver instance for chromedriver
IWebDriver driver = new ChromeDriver();
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
driver.Manage().Window.Size = new Size(1100, 1100);
driver.Navigate().GoToUrl(hehe[i]);
js.ExecuteScript("return document.body.style.overflow = 'hidden';");
This worked!
Related
Selenium fresher:
I used the following code in selenium to open the print window but it doesn't work
Keyboard keyboard = ((HasInputDevices)driver).getKeyboard();
keyboard.pressKey(Keys.ENTER);
keyboard.pressKey(Keys.chord(Keys.CONTROL, "p"));
And i used the following code to open the Print window and it works
((JavascriptExecutor) driver).executeScript("print()");
My Question is how to print the page and close the print window?
You can use the kiosk-printing chrome option to complete the print without interacting with print window.
Here is the snippet that you can use.
Java:
ChromeOptions options = new ChromeOptions();
options.addArguments("--kiosk-printing");
WebDriver driver = new ChromeDriver(options);
driver.get("https://google.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.print();");
Python:
options = webdriver.ChromeOptions()
options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://google.com")
driver.execute_script('window.print();')
You will see the print window for a fraction of seconds and then it will close automatically. Then you can see the printing job in your default printer.
I am attempting to use ctrl + click on a link to open it in a new tab. This is working fine in Chrome 58. Please find the code below:
action.keyDown(Keys.CONTROL).click(driver.findElement(By.xpath
("//section[#class='filmStrip__basic']//a[text()='En savoir
plus']"))).keyUp(Keys.CONTROL).build().perform();
I am using the same code on IE, Firefox and Safari but getting the following error:
Firefox 54: The link is getting open in the same tab.
IE 11: Nothing happening.. the control is moving to the next line
Safari: exception on action.keyDown-Unrecognized command
Help related to any one browser is also appreciated.
Thanks
As you are trying to click on a link which is within a <a> tag, instead of xpath you can use the linkText locator. Here is the sample code with opens the url http://www.google.com, verifies the Page Title, uses Actions class to click on the Gmail link to open https://accounts.google.com in a new tab.
String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
You can find a relevant Python based solution in How to open a link embed in web element in the main tab, in a new tab of the same window using Selenium Webdriver
Another way is to use javascript executor:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.open('','_blank');");
As for your problem I had it too and didn't find anything usefull until I found this workaround.
I even tried: solution with ctrl + enter
try this way....
// specify chromedriver.exe directory path and replace in "driverPath"
String driverPath = "C:/Users......";
WebDriver driver;
System.setProperty("webdriver.chrome.driver", driverPath + "chromedriver.exe");
driver = new ChromeDriver();
System.out.println("lanuching 1st url in tab1");
driver.navigate().to(
"https://amazon.com");
System.out.println("lanuched 1st url in tab1");
Thread.sleep(30000);
((JavascriptExecutor) driver).executeScript(
"window.open('http://ebay.com');");
Thread.sleep(20000);
Set<String> allwh = driver.getWindowHandles();
System.out.println(allwh.size());
for (String v : allwh) {
System.out.println(v);
driver.switchTo().window(v);
String title = driver.getTitle();
System.out.println("2nd url in tab2" + title);
I wrote a Selenium test with java that I launched with the FirefoxDriver and it is executing fine in Firefox browser.
Then I am replacing FirefoxDriver with HtmlunitDriver like this:
driver = new FirefoxDriver();
with
driver = new HtmlUnitDriver(true);
But then I got this error :
It's missing ';' Before an instruction (http://local.project/bundles/app/js/socket.js#1)
This is the socket.js file :
class SocketHandler {
constructor(url) {
this.url = url;
this.session = null;
}
....
}
I suspect that it doesn't recognize the class declaration. Any idea how to correct that please?
You don't even need to use PhantomJs. As PhantomJs is not so much maintain these days. You can use chromedriver in headless mode.
you just need to add options as headless as below :-
chromeOptions.addArguments("--headless");
Please find complete code below:
System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://google.com");
While if still you want to use phantomjs. then first download phantomjs binary from below location :-
http://phantomjs.org/download.html
Now use below code :-
System.setProperty("phantomjs.binary.path","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\phantomjs\\phantomjs.exe");
DesiredCapabilities capabilities = null;
ArrayList<String> cliArgsCap = new ArrayList<String>();
capabilities = DesiredCapabilities.phantomjs();
cliArgsCap.add("--web-security=false");
cliArgsCap.add("--ssl-protocol=any");
cliArgsCap.add("--ignore-ssl-errors=true");
capabilities.setCapability("takesScreenshot", true);
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,new String[] { "--logLevel=2" });
driver = new PhantomJSDriver(capabilities);
driver.get("https://www.google.co.in/");
Hope it will help you :)
I did a simple demo when useing JxBrowser Calling Java from JavaScript, and then I found the phenomenon,
public static void main(String[] args) {
// TODO Auto-generated method stub
BrowserPreferences.setChromiumSwitches("--remote-debugging-port=9222");
Browser browser1 = new Browser();
BrowserView browserView1 = new BrowserView(browser1);
// Gets URL of the remote Developer Tools web page for browser1 instance.
String remoteDebuggingURL = browser1.getRemoteDebuggingURL();
browser1.addScriptContextListener(new ScriptContextAdapter() {
#Override
public void onScriptContextCreated(ScriptContextEvent event) {
Browser browser = event.getBrowser();
JSValue window = browser.executeJavaScriptAndReturnValue("window");
window.asObject().setProperty("java", new JavaObject());
}
});
JFrame frame1 = new JFrame();
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame1.add(browserView1, BorderLayout.CENTER);
frame1.setSize(700, 500);
frame1.setLocationRelativeTo(null);
frame1.setVisible(true);
browser1.loadURL("https://www.baidu.com");
// Creates another Browser instance and loads the remote Developer
// Tools URL to access HTML inspector.
Browser browser2 = new Browser();
BrowserView browserView2 = new BrowserView(browser2);
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame2.add(browserView2, BorderLayout.CENTER);
frame2.setSize(700, 500);
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);
browser2.loadURL(remoteDebuggingURL);
}
I run the code and refresh the chrome, many times and then
before refresh
refresh 2 Minute
if user other network with more resources or images ,The Memory increase more faster, Would like to ask whether this problem can be solved
thanks all!
I'm looking forward to the reply!
I checked your example and I confirm that it is an issue in JxBrowser. I've created the corresponding task in our issue tracking system. We will fix this issue in one of the next JxBrowser versions. I will let you know when the new build with the fix is available for download.
I am trying to disable javascript in selenium using noscript extension, as suggested here -->How to disable Javascript when using Selenium by JAVA?
But, it looks like it no longer works,
Here is what I have written :
FirefoxProfile profile = new FirefoxProfile();
File extPath = new File("noscript.xpi");
profile.addExtension(extPath);
//profile.setPreference("javascript.enabled", false);
WebDriver driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://enable-javascript.com");
Even tried it by loading a profile that has javascript disabled, that doesn't seem to work either.
Code :
File profileDirectory = new File("Profiles/4hsi6txm.testing");
FirefoxProfile profile = new FirefoxProfile(profileDirectory);
DesiredCapabilities cap = DesiredCapabilities.firefox();
//
cap.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver = new FirefoxDriver(cap);
driver.get("http://enable-javascript.com");
I am trying it on Firefox version-43.0.1 and selenium version-2.48.2
Any fix for this ?
Thanks :)