Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right click on a element and select a option in chrome browser in selenium

I am automating a page, where i want to right click a element and then click "Save link as.... I am able to right click on the element but not able to select any option. Below is the code I have written, but instead of selecting a option it actually clicks on that element.

WebElement elementq =driver.findElement(By.xpath("//a[contains(text(),'fedev.docs-gmail.JPG')][@class]"));
      Actions builderq = new Actions(driver);
    builderq.contextClick(elementq).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER);
      builderq.build().perform();

Below is the HTML:

<p><a class="icon icon-attachment" href="/attachments/download/15535/fedev.docs-gmail.JPG">fedev.docs-gmail.JPG</a>  
  <span class="size">(100 KB)</span>
    <a data-confirm="Are you sure?" class="delete" title="Delete" rel="nofollow" data-method="delete" href="/attachments/15535"><img src="/images/delete.png" alt="Delete"></a>
    <span class="author">Asim Sarfraz, 25 September 2018 08:24 AM</span>
  </p>
like image 742
Mansi Avatar asked Dec 31 '25 22:12

Mansi


1 Answers

If your aim is to download the image from the web page, instead of right click you can download the image with url directly.

WebElement elementq =driver.findElement(By.xpath("//a[contains(text(),'fedev.docs-gmail.JPG')][@class]"));
String imageLink = logo.getAttribute("href");
String fileName = url.getFile();
URL imageURL = new URL(imageLink);
InputStream in = new BufferedInputStream(imageURL.openStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));

for ( int i; (i = in.read()) != -1; ) {
    out.write(i);
}
in.close();
out.close();
like image 166
Navarasu Avatar answered Jan 03 '26 10:01

Navarasu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!