Sending a keyboard chuck to chrome and how to print in kiosk mode so that you don't have to confirm your printing chrome will just print using your default settings.
Then we'll look at what you need to do if you want to change your print settings by referencing the print settings page and how to change various settings by doing things like selecting.
From drop-down lists and entering values into inputs finally we'll show you how to click the print button to print what you've requested using the settings you've.
Applied so let's get started to get started i've created a new excel workbook and in the visual basic editor i've already written some fairly standard basic code to open a new.
Instance of chrome and then navigate to wikipedia's main page this is dependent of course on having set a reference to the selenium type library if i head to the tools menu and.
You're following along with this video but just in case you haven't there's a quick video that explains how to get set up and running with selenium basic having done all that already let's give.
Our code a quick test run to make sure we can open up our new instance of chrome which we do and we navigate to wikipedia's main page and the next thing i'd like to do is.
Programmatically send the keyboard shortcut control and p to that web page so if i press ctrl and p while chrome is displayed on screen you'll probably know that it fires up the print dialog box so.
We'd like to do that programmatically so i'll close down that instance of chrome to add that line to my subroutine let's uh give myself a blank line to give myself a bit of space.
One thing that's quite important here is that we don't use the send keys method of the chrome driver you may have seen in previous videos me using the send keys method by saying.
Cd.send keys for this example i want to
Make sure we're using the vba send keys method so don't precede this with a reference to the chrome driver simply say send keys followed by a space.And then in some double quotes the code required to send a control key and the p key at the same time the correct is the symbol that simulates the control key followed by the letter p.
Close the double quotes and then if we run the subroutine again we'll see we open up chrome navigate to wikipedia and then it fires up the print dialog box automatically.
Let's just close that down and then move on to the next bit just a quick note on why i didn't use the send keys method of the chrome driver object the fairly simple reason.
Is that it doesn't appear to work or at least i couldn't get it to work just to demonstrate i'm going to declare a variable that holds a reference to a new instance of a selenium keys class so.
I'm going to say dim ks as new selenium dot keys and then i'm just going to comment out the send keys method i've written for vba and i'm going to say cd dot send.
Keys the modifier key that i'm going to use is going to be ks dot control followed by a comma and then the key i'll pressing combination with the.
Control key is the letter p now in principle that should work but it seems to have absolutely no effect whatsoever when we run the subroutine so the keyboard shortcut has been sent it's.
Just not influenced the chrome driver i'm not entirely sure why but i just know that that doesn't work whereas the vba send keys method does so i'm going to get rid of the cd.send.
Keys line and bring back the vba version
I'm going to keep my variable for the selenium keys class though we will use that a little later on in the video when things get a bit more complicated so.Let's keep that in there for now and then move on and look at how to actually get that page to be printed by far the easiest way to get this page printed is just to assume that we want.
To use all the default settings which appear in the print dialog box and when we press control and p to have the page printed immediately now the default settings you'll see in.
The dialog box will depend on which profile you're using for the instance of chrome you've opened up at the moment we're not specifying a profile so we're using chrome.
Anonymously if you did want to use a specific profile to get access to your saved print settings then we do have a quick video which explains how to open chrome using a specific user profile but.
For this one we're going to continue with the anonymous profile so to do this we need to set an argument of the chrome driver before we start it so let's add a couple of blank lines and.
Say cd dot add argument and then in some double quotes i'm going to type in double hyphen and then kiosk dash printing so that will basically prevent chrome.
From waiting for us to confirm that we want to print something if i just run the subroutine now we will see the print dialog box open briefly but then it immediately sends that to.
The printer and of course it tells me that my printer's offline because i'm not currently connected to it so my printer called tinkerbell is not available at the moment but if i just.
Double click on the printer icon in the system tray you can see that i've sent the wikipedia main page to the printer let's just right click and cancel that document though because i don't want to.
Have that printed out when i do finally reconnect to that printer so that's the easy way to do it and wouldn't it be wonderful if we could just leave it at that but of course.
There are going to be times where you'll want to change some of those default print options and this is where things get a bit tricky just to show you the first thing we'll.
Need to do this is actually one of the easier things if i head up over to an open version of the wikipedia main page and press control and p you can hopefully see that the print.
Dialog box opens in a separate window altogether so the first thing we need to do is switch to that window i'll just cancel out of that and show.
You that we do have a video which explains this in a bit more detail as part of our getting data from websites playlist working with multiple tabs in selenium this one's fairly.
Straightforward though if i switch back to the visual basic editor first of all i'm going to comment out the kiosk printing argument and then after sending keys we're going to say.
Simply cd dot switch to next window that means that when we start searching for elements in a moment we'll be searching for elements in the print dialog box not the wikipedia main page.
So now we can start searching for elements which represent the various print options and this is where things get tricky just to demonstrate i'm going to switch back to the wikipedia main.
Page in an open instance of chrome and if i press ctrl and p to launch the print dialog box let's start by trying to just change the layout of the page from portrait to.
Landscape if i right click on that drop down list element and choose to inspect it we'll see that it's simply a reference to a select element now we've created a video.
Fairly recently which explains how to work with select drop-down lists in selenium so the drop-down list itself is pretty straightforward.
What is a bit tricky however is how that layout drop-down list element is nested within all of the other elements so you can see that it's nested inside a print preview settings element.
Which itself is nested inside a print preview layout settings element which has a shadow root now again in a previous video you may remember how awkward it is to get references to.
Elements in the shadow document object model so what makes this even trickier is how many different levels of shadow roots there are so print preview layout.
Settings is held inside the print preview sidebar with its shadow root which itself is held inside the print preview app with a shadow root so you've got quite a lot of levels to.
Walk through to get to that drop down list the sidebar is quite an important one for us most of the other elements we're going to access are held within this one.
So to begin with let's try to get a reference to the sidebar element and store that in a variable so let's switch back to the visual basic editor and look at how we can do that.
We'll start by declaring a variable to hold a reference to the sidebar element so let's say dim i'm going to call mine something like prv short for preview or print preview.
Sidebar as a selenium dot web element then after we've switched to the next window we can say set prv sidebar equals cd.executescript.