2

I use selenium to test a webpage . I want to display an image from that webpage on a JOption pane . How can I do this ? Here is the code

WebDriver driver = new FirefoxDriver() ; 

driver.get(http://.........com)  ;

WebElement imageElement = driver.findElement(By.id("imageID")) ; 

JOptionPane.showInputDialog // ?

How to include this image from a website to a JOption Pane ? DO I need to download it first ?

Borat Sagddiev
  • 807
  • 5
  • 14
  • 28
CHEBURASHKA
  • 1,623
  • 11
  • 53
  • 85
  • check answer here http://stackoverflow.com/questions/13963392/add-image-to-joptionpane – Abhishek Nayak Feb 10 '14 at 06:16
  • @CHEBURASHKA `ImageIcon` can use a URL so perfect for what you want (generally though you dont want to rely on an external address for resources) – Reimeus Feb 24 '14 at 00:02

2 Answers2

1
ImageIcon icon = new ImageIcon("c://photo.jpg");
JOptionPane.showMessageDialog(null, "message", "title", JOptionPane.OK_OPTION, icon);
Ashish
  • 1,943
  • 2
  • 14
  • 17
1

First we must find the element. Note that the element must be the <img> element.

WebElement imageElement = driver.findElement(By.id("imageID"));

Next, we get the url of the image within that element:

String imagePath = imageElement.getAttribute("src");

We convert it into a URL:

URL imageUrl = URL(imagePath);

Then we read it into an image:

Image image = ImageIO.read(imageUrl);

Finally, we put it in an JOptionPane:

JOptionPane.showMessageDialog(null, "message", "title", JOptionPane.OK_OPTION, new ImageIcon(image));

Now, please note that I have split the code into separate lines because I want you to actually understand what the code is doing. None of the steps are particularly hard, and would have definitely been possible with some google searches.

Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56