How do I select a Right Click Menu Item on Browser Menu with Protractor/Javascript?

1k views Asked by At

I am trying to select "Back" after performing a Right Click. I can get the Right-Click menu. I am getting an error when I try to select "Back". Here is the code:

browser.driver.findElement(By.linkText("Back")).click();

I am using Chrome browser on MAC-OS.

Here is the error:

Message: NoSuchElementError: no such element (Session info: chrome=43.0.2357.130) (Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf),platform=Mac OS X 10.10.3 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 23 milliseconds

2

There are 2 answers

1
Pratik Shah On

You can use following jQuery code to detect Right Mouse Click

$('#element').mousedown(function(event) {
    switch (event.which) {
        case 1:
            alert('Left Mouse button pressed.');
            break;
        case 2:
            alert('Middle Mouse button pressed.');
            break;
        case 3:
            alert('Right Mouse button pressed.');
            break;
        default:
            alert('You have a strange Mouse!');
    }
});

And that you can call following code to perform back button action (or go to lastly visited page)

window.history.back();

Hope this is what you are looking for.

3
nilesh On

It could be possible that you are trying to click on the menu before it appears.

var driver = browser.driver,
 wd = browser.wd,
 until = wd.until,
 elementToRightClick = driver.findElement(wd.By.linkText('foo'));
 driver.actions().click(elementToRightClick,wd.Button.RIGHT).perform(); 
 driver.wait(until.elementIsVisible(driver.findElement(wd.By.linkText('back')),5000, 'Element back was not visibile');
 driver.findElement(wd.By.linkText('back')).click();