Mansi
Mansi

Reputation: 73

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>

Upvotes: 3

Views: 2187

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193338

Too big as a comment so publishing as an answer.

As per your question you wanted to contextClick(element) and select Save link as through Actions.

It is worth to mention if the context-menu-list had been implemented as a native context-menu-list we could have solved that pretty easily. But as per the HTML you have shared it seems the context-menu-list is Chrome Browser's native context-menu-list.

As per Chromedriver does not send keys to context menu:

  • contextClick(element) goes to the renderer process.
  • It is a limitation in the way ChromeDriver simulates key presses.
  • Can be termed as a bug or a limitation of ChromeDriver's architecture and we have to wait till the issue is being resolved.

Upvotes: 0

Navarasu
Navarasu

Reputation: 8489

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();

Upvotes: 1

AutomatedOwl
AutomatedOwl

Reputation: 1089

Try to solve it using Robot object:

Actions builderq = new Actions(driver);
builderq.contextClick(elementq);
builderq.build().perform();
Robot robot = new Robot();
int REPEATS_OF_VK_DOWN_INPUT = 3;
for (int i = 0; i < REPEATS_OF_VK_DOWN_INPUT; i++) {
    robot.keyPress(KeyEvent.VK_DOWN);
    robot.keyRelease(KeyEvent.VK_DOWN);
    Thread.sleep(500);
}
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(1000);

Upvotes: 0

Related Questions