Im working on a project that takes a screenshot of a product image.
This image is then cropped and saved.
Here is the code
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
using (var ms = new MemoryStream(screenshot.AsByteArray))
using (var imgShot = Image.FromStream(ms))
using (var src = new Bitmap(imgShot))
{
IWebElement element = driver.FindElement(By.Name("viewport"));
Rectangle cropRect = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
var clone = src.Clone(cropRect, src.PixelFormat);
clone.Save(_SavePath);
}
Referring to the image link below, How would I use C# Selenium driver.FindElement in order to select this image:
This is what I'm trying to use:
IWebElement element = driver.FindElement(By.Name("viewport"));
Image:
http://dealer.rectron.co.za/ImageServer.aspx?QualifyingProductID=c471d4fd-fd97-48b8-a709-441b18c1830c
Here is the HTML Code of the Image:
<html><head><meta name="viewport" content="width=device-width, minimum-scale=0.1"><title>ImageServer.aspx (335×328)</title></head><body style="margin: 0px; background: #0e0e0e;">[![][1]][1]</body></html>
When I try to crop the image, I get the following exception:
System.ArgumentException: 'Rectangle '{X=0,Y=0,Width=0,Height=0}' cannot have a width or height equal to 0.'
I think that they way I'm using FindElement, might be incorrect.
Thanks
Your locator is incorrect, you are creating a <meta> element that does not appear on the page and has no dimensions. You need to fix the locator as follows: IWebElement element = driver.FindElement(By.XPath("//img"));
Related
I need to add some text content to the bottom of an existing .pdf document with iText. I have a working method, but the new content is displayed in the top-left corner, overlapping the existing content:
public PdfDocument GetDocumentWithAppendedContent(string path, string content)
{
var stream = new MemoryStream();
var writer = new PdfWriter(stream);
writer.SetCloseStream(false); // so I can reuse stream to create a readonly document later
var pdfResult = new PdfDocument(new PdfReader(path), writer);
var document = new Document(pdfResult);
var div = new Div().SetMargin(0).SetPadding(0).SetKeepTogether(true);
div.Add(new Paragraph(content));
document.Add(div);
document.Close();
pdfResult.Close();
stream.Position = 0;
return new PdfDocument(new PdfReader(stream)); // return a readonly version
}
How to "move" the content at the bottom of the last page, instead of the beginning of the first? I am new to iText and, surprisingly, can't find a solution online.
You can do it using the code like this.
PdfPage page = doc.GetPage(doc.GetNumberOfPages());
//Create canvas fro the last page
Canvas canvas = new Canvas(page, page.GetPageSize());
//Set fixed position to put the div at the left bottom corner of the canvas
div.SetFixedPosition(0, 0, page.getPageSize().getWidth());
canvas.Add(p);
doc.Close();
I'm working on rendering an image on a pdf document. The rendering is done in C# method like this:
public void PrintPdf(System.Drawing.Image image, string htmlCode, string url)
{
PdfDocument Document = new PdfDocument();
Document.Margins = new PdfDocumentMargins(20, 20, 20, 20);
Document.ImagesCompression = 0;
Document.Compress = false;
Document.SerialNumber = "our-serial-number";
PdfPage page = Document.AddPage(PdfPageSize.A4, Document.Margins, PdfPageOrientation.Portrait);
PdfHtml overlayHtml = new PdfHtml(0, 0, htmlCode, url);
overlayHtml.BrowserWidth = 740;
overlayHtml.MediaType = "print";
PdfLayoutInfo layoutInfo = page.Layout(overlayHtml);
PdfImage pdfImage = new PdfImage(0, (float)115.5, image);
layoutInfo = page.Layout(pdfImage); // <-- this row causes exception from the HiQPdf library
}
The last row throws the following exception from the HiQPdf library. Any thoughts on why?
[HiQPdfException: Cannot layout the image]
HiQPdf.PdfImage.LayoutObject(PdfCanvas objectsContainer) +7243
HiQPdf.PdfPage.Layout(PdfObject pdfObject) +86
This error started happening after we updated HiQPdf version from 5.4.0.0 to 10.17.0.0
The parameters seem ok, there is normal html in htmlCode and I don't see anything wrong with the image either. The image itself can be downloaded as png well as pdf, which works fine.
I need to take a screenshot of a whole element in selenium C# using chromedriver. The element is table and though I am getting the width and height of the element, the screenshot I am getting is of only 15 rows.
IWebElement element = driver.FindElement(By.XPath("Xpath of the element"));
string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".jpg";
Byte[] byteArray = ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;
System.Drawing.Bitmap screenshot = new System.Drawing.Bitmap(new System.IO.MemoryStream(byteArray));
System.Drawing.Rectangle croppedImage = new System.Drawing.Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
screenshot = screenshot.Clone(croppedImage, screenshot.PixelFormat);
screenshot.Save(String.Format(#"path" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg));
Is there any way other than this from which I can get the screenshot of the whole table scrolling the webpage?
Does the table have an ID? If so you could take a screenshot of the table using that id rather than the element name?
WebElement ele = driver.findElement(By.id("yourTableID"));
From the similar question (but not Xpath):
How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?
Seems to suggest you might need to force the window to be larger before taking the screenshot?
this.driver.manage().window().setSize(new Dimension(1680, 1050));
You can use this package https://www.nuget.org/packages/Noksa.WebDriver.ScreenshotsExtensions/
In order to take a screenshot of the element, use the OnlyElementDecorator:
using WDSE;
using WDSE.Decorators;
using WDSE.ScreenshotMaker;
var vcs = new OnlyElementDecorator(new ScreenshotMaker());
var screen = _driver.TakeScreenshot(vcs);
I am working with c# now stored webpage content in single variable and I have one text box if I paste any URL that will show the full source code the link .now I want to find all the image tags where it is begin and where it is finished.also I like to merge except image tags .
can you anyone tell me how to do..
Assuming you want to parse the content server-side you can use the Html Agility pack
See this question
Try This:
var images = doc.DocumentNode.SelectNodes("//img");
if (images != null)
{
foreach (HtmlNode image in images)
{
var alt = image.GetAttributeValue("alt", "");
var nodeForReplace = HtmlTextNode.CreateNode(alt);
image.ParentNode.ReplaceChild(nodeForReplace, image);
}
}
var sb = new StringBuilder();
using (var writer = new StringWriter(sb))
{
doc.Save(writer);
}
We have an application that reads Word documents and imports them into our file format.
A recent bug was found that the page count is only available in Page Layout View, however Word 2010 defaults to Web Layout.
Using .NET c# how can we change the view to give us back the page count?
I believe the property you are looking for is Document.ActiveWindow.View.Type = wdPrintView; You can read more in this on MSDN.
//Here is my code snipped based on 'DocumentFormat.OpenXml' nuget package.
//Open doc file
using (var wordDocument = WordprocessingDocument.Open(strInputFile,true))
{
SectionProperties sectionProps = new SectionProperties();
//Set page margins
PageMargin margin = new PageMargin() { Top = 1008,
Right = (UInt32Value)1008U,
Bottom = 1008,
Left = (UInt32Value)1008U,
Header = (UInt32Value)720U,
Footer = (UInt32Value)720U, `enter code here`
Gutter = (UInt32Value)0U };
sectionProps.Append(margin);
//Apply margin
wordDocument.MainDocumentPart.Document.Body.Append(sectionProps);
//Save changes
wordDocument.Save();
}