Javascript equivalent of contextclick?

643 views
Skip to first unread message

Konto Prywatne

unread,
Nov 8, 2016, 4:30:29 AM11/8/16
to Selenium Users
Hello all,

I am trying to right click on an image and save it from context menu, but i am using the JAVASCRIPT bindings for selenium Webdriver (Webdriver JS). 

This code doesn't work:


driver
.wait(until.elementLocated(By.css('#res348022787 img')), 10000, 'Could not locate element');
var testImg = driver.findElement(By.css('#res348022787 img'));
    testImg
.click(button.RIGHT);
    driver
.sleep(2000);
    driver
.actions()
   
.sendKeys(webdriver.Key.ARROW_DOWN)
   
.sendKeys(webdriver.Key.ARROW_DOWN)
   
.sendKeys(webdriver.Key.ARROW_DOWN)
   
.sendKeys(webdriver.Key.ARROW_DOWN)
   
.perform();

The right click never seems to happen so eventually the page just scrolls down.

I saw a Java solution here: http://www.codeproject.com/Articles/856324/Perform-right-click-action-using-WebDriver, but I can't get this to work in the javascript version. Here is her code:



public class Right_click {

public static void main(String args[]) throws Exception{

 
// Initialize WebDriver
 
WebDriver driver = new FirefoxDriver();
 
// Wait For Page To Load
 driver
.manage().timeouts().implicitlyWait(120,TimeUnit.SECONDS);
 
 
// Go to Myntra Page
 driver
.get("http://www.myntra.com/");
 
 
// Maximize Window
 driver
.manage().window().maximize();
 
 
WebElement R1 = driver.findElement(By.xpath("//a[@href='/shop/men?src=tn&nav_id=5']"));
 
 
// Initialize Actions class object
 
Actions builder = new Actions(driver);
 
 
// Perform Right Click on MEN and Open "Men" content in a new tab
 builder
.contextClick(R1).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
 
//ContextClick() is a method to use right click
 
 
/* Perform Right Click on MEN and Open "Men" content in a new different Window
 
 builder.contextClick(hindiLanguage).sendKeys
 (Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
 
 //closing current driver window
 driver.close();
 */

 
}
}

David

unread,
Nov 8, 2016, 4:32:09 PM11/8/16
to Selenium Users
If you could, you might want to try with Java bindings first, against your site/app. This will at least narrow down whether it's an issue getting it to work against your site/app, or if it's an issue with the javascript binding for WebDriver, since we "assume" it should work with the Java reference example. If neither work, then it might be a compatibility issue trying to do context click on your site via automation. I say this since Java is typically the defacto reference binding for WebDriver/Selenium functionality, with others as 2nd, 3rd class citizens, etc.

Louis Duran

unread,
Jan 19, 2017, 10:51:38 PM1/19/17
to Selenium Users
I'm curious to know if you have solved this problem using the Javascript API.

I am trying to do a similar thing. I have a protractor test that runs the spell check in a TextArea field. I can select a misspelled word and bring up a context menu with a list of suggested spellings. The menu appears but I can not select any item in that list. My code looks like this:

            actions.mouseMove(textArea).perform();
            actions.click().perform();
            actions.sendKeys(Key.chord(Key.SHIFT, Key.CONTROL, Key.ARROW_LEFT)).perform();

            actions.click(protractor.Button.RIGHT).perform();

            // select the first suggestion (should be misspelling)
            actions.sendKeys(Key.ARROW_DOWN).sendKeys(Key.RETURN).perform(); // <-- THIS FAILS! CAN'T SELECT ANYTHING IN THE MENU

            expect(textArea.getAttribute("value")).toContain(spelledNote);

And, by the way, the menu is not a custom DOM element menu. It is just the native context menu that comes up in Chrome (Electron actually)

Thanks,
Louis

On Tuesday, November 8, 2016 at 1:30:29 AM UTC-8, Konto Prywatne wrote:
Reply all
Reply to author
Forward
0 new messages