Unable to extract text from Popup window - selenium

I am unable to extract or get the text from the popup Window. I am using the below code:
Driver.findElement(By.xpath("xpath of popup")).getText();
It is a div image popup (if that helps). I want get the text out the window and print it in the console. When I use the above code it just passes the test and does not get the text from the image.

You can't simply get text from an image. To get text from an image, you need OCR (optical character recognition).
The following should give you a starting point:
URL url = new URL(imageUrl);
Image image = ImageIO.read(url);
String text = new OCR().recognizeCharacters((RenderedImage) image);

Related

Selenium AShot: Why Screen shot of an particular element is white

On this webpage: https://www.selenium.dev/downloads/
I am trying to capture the screenshot of an element that is an img tag.
You can inspect : img[alt='Java']
This is the img element I want to capture:
Element to capture
So to capture the above element I wrote this code:
WebElement imgJava = driver.findElement(By.cssSelector("img[alt='Java']"));
Screenshot screenshotJava = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver, imgJava);
ImageIO.write(screenshotJava.getImage(), "png",new File(System.getProperty("user.dir") + "\\javaImg.png"));
But when I opened the image, it is just all white:
Image after caputring
And if I remove the shooting shotingStrategy and just use new AShot().takeScreenshot(driver,imgJava);
It will take screenshot of whole page even though I passed a particular element:
Whole page

Ignore screen reader text in serenity

serenity is returning the screen reader text along with the visible text in the screen. How to get text which is visible to the user?
public static Target TransactionTypeOption = Target.the("Transaction Type options").located(By.xpath("//ipb-dropdown[#class='type-dropdown ']//li//div"));
List<String> options = Text.of(TransactionTypeOption).viewedBy(actor).asList();
I have used bootstrap Sr-only class for the screen reader text.
So, I just wanted to read the first content which is "All Transactions" but serenity is returning me "All Transactions white spaces , current selection"
The XPath you are using is selecting the div, which also picks up some text under the div, which is , current selection in this case. I've seen this issue before with Selenium, and the only solution is to handle the extra text in your code.
public static Target TransactionTypeOption = Target.the("Transaction Type options").located(By.xpath("//ipb-dropdown[#class='type-dropdown ']//li//div"));
string textToFilterOut = driver.findElement(By.xpath("//span[#class='sr-only']")).getText();
List<String> options = Text.of(TransactionTypeOption).replace(textToFilterOut, "").viewedBy(actor).asList();

How to get the path of an Image Field in a pdf form, in Adobe?

I am creating a PDF form using adobe reader. I have added an image field and a text box. The text box is read-only and I want to populate the text box with the path of the image selected by the end-user, in the image field. Following is my code:
var one = this.getField("Image1");
var two = this.getField("Text1");
two.value='The Path';
The above code runs normally but I can't figure out as to what to write instead of 'The Path', to get the actual path of the image selected by the end-user.
P.S.
On the Image1 button there are 2 actions:
Mouse Up(execute Js)
event.target.buttonImportIcon();
On Blur(execute Js)
var one = this.getField("Image1");
var two = this.getField("Text1");
two.value='The Path';
If I am understanding your request correctly... Assuming that Image1 is a button field and Text1 is a text field and you want the selected image file to appear as the button icon, the code would be as follows...
var one = this.getField("Image1");
var two = this.getField("Text1");
var doc = app.browseForDoc(); // Allows the user to browse for a file
var docPath = doc.cPath; // gets the file path of the selected file
one.buttonImportIcon(docPath); // uses the selected path to import the image as the "normal" or "up" button icon
two.value = docPath; // set the value of the text field to the selected device independent path

Cannot open embedded hyperlink in PDF generated using PDFBox

I have used PDFBox version 2.0 to generated a PDF containing a clickable URL.
// Create a new annotation and make it invisible
PDAnnotationLink txtLink = new PDAnnotationLink();
txtLink.setInvisible(true);
// Add an action
PDActionURI action = new PDActionURI();
action.setURI(url);
txtLink.setAction(action);
// Create a new rectangle that will be the clickable area
PDRectangle position = new PDRectangle();
position.setLowerLeftX(currentXpos);
position.setLowerLeftY(currentYpos - rectangleHeight);
position.setUpperRightX(currentXpos + rectangleWidth);
position.setUpperRightY(currentYpos);
// Write the "Link" string in blue
contentStream.setNonStrokingColor(Color.blue);
contentStream.showText(elm.text());
contentStream.setNonStrokingColor(Color.black);
// Make the rectangle a clickable link and add it to the page
txtLink.setRectangle(position);
page.getAnnotations().add(txtLink);
When I click on the generated PDF in Chrome 45, the document is opened in the Chrome's PDF viewer. The link is clickable, no problem.
If I click on the generated PDF in Firefox (41.0.1) or IE 11, the document is loaded in the Adobe PDF viewer plugin and the link is not clickable. The mouse-over displays the correct URL, but nothing happens when I click the link.
Is this a security issue? Is there anything I can do in the PDFBox code to make the link clickable always?
I was able to hide the border by setting the width to 0:
// Create a new annotation and make it visible
PDAnnotationLink txtLink = new PDAnnotationLink();
txtLink.setInvisible(false);
// Set the border to zero to hide it
PDBorderStyleDictionary border = new PDBorderStyleDictionary();
border.setWidth(0);
txtLink.setBorderStyle(border);

How to Deal with HtmlUnit and Frames. Tricky Frame Logic

My original question was confusing, I rewrote it. Now it is much clearer.
Hello I am trying to navigate web page using HtmlUnit. I am trying to
go to url -> enter text in text box (in frame 1) -> Press Search Button (in frame 1) -> go to new frame (frame 2) that appeared as result of search.
When I write this code:
HtmlPage page = webClient.getPage("http://someurl.com");
HtmlPage toolbar = (HtmlPage) page.getFrameByName("toolbar").getEnclosedPage(); //GET TOOLBAR FRAME
HtmlTextInput searchBox = toolbar.getElementByName("query"); //GET TEXT BOX ELEMENT
searchBox.setValueAttribute("SOME STRING"); //FILL TEXT BOX ELEMENT WITH TEXT
List<HtmlElement> elements1 = (List<HtmlElement>) toolbar.getByXPath(<SOME XPATH>); //LIST OF A SINGLE ELEMENT WHICH IS THE SEARCH BUTTON
HtmlElement element2 = elements1.get(0); // GET SEARCH BUTTON ELEMENT FROM LIST
page = element2.click(); // CLICK SEARCH BUTTON AND LOAD NEW PAGE TO "page"
I am searching for "SOME STRING". When I do this, another page comes up (though does not refresh the page, the URL never refreshes) with an ADDITIONAL frame. So now, I click the search button but thew new page is just a webpage of the toolbar frame, not of the whole webpage.
I want to now access the additional frame. So how do I "back out" of the current frame and select the new frame?
I found out the answer my trial and error.
page = (HtmlPage) searchButton.click().getEnclosingWindow().getTopWindow().getEnclosedPage();
page = (HtmlPage) page.getFrameByName("Frame 1").getEnclosedPage();
page = (HtmlPage) page.getFrameByName("Frame 2").getEnclosedPage();
It works great, hope I can help some of you out.