0

I'm facing an issue while automating right-click and then selecting any options from it.

The code is working fine in Firefox but not in Chrome.

There, it seems it is just showing a right click but doing a normal click on the element.

I am using chromedriver 2.21 and Selenium 2.50.

package mentor.qa.selenium;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Stack {
    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");

        // WebDriver driver = new FirefoxDriver();
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com/");

        WebDriverWait wait = new WebDriverWait(driver, 30);
        Thread.sleep(3000);

        WebElement b = driver.findElement(By.linkText("About"));
        Actions action = new Actions(driver);
        action.moveToElement(b);
        Thread.sleep(4000);

        // Keys move = Keys.ARROW_DOWN;
        // action.contextClick(b).sendKeys(move).sendKeys(Keys.ENTER).build().perform();

        action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
        Thread.sleep(4000);

        action.click().build().perform();
        // action.keyDown(Keys.CONTROL).click().build().perform();
        // action.contextClick(b).sendKeys(Keys.CONTROL).keyDown(Keys.CONTROL).sendKeys(String.valueOf(d)).sendKeys(Keys.ENTER).build().perform();
        // action.click();
        // action.contextClick(b);
        Thread.sleep(4000);

        // action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
        // driver.findElement(By.partialLinkText("new tab")).click();
        // action.sendKeys(Keys.RETURN).perform();
        Thread.sleep(4000);

        //driver.quit();
    }
}
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
dheeraj kateja
  • 161
  • 2
  • 4
  • 13
  • Please show your code. – Andrew Regan Feb 19 '16 at 21:25
  • I am unable to put my code here.Instead it is asking me to format it and al.Cant paste my code in any way here.Not sure why thr r so many restrictions even to paste the code!!If u have any other way,plz suggest – dheeraj kateja Feb 20 '16 at 06:02
  • @Dheeraj since you posted the question you should be able to edit it. edit and post code in question instead of comments. Also copy your code into notepad. Select all and select Code formatting using the symbol ` on both sides and just paste it. its easy – Makjb lh Feb 20 '16 at 19:49
  • I have added the code in question.Its working perfectly in firefox but not in chrome.The issue is that chrome cant identify Keys.arrow_down commands.Do i need to add anything in order for chrome to identify it. – dheeraj kateja Feb 20 '16 at 20:07
  • OK gotcha - the Keys - which does not apply to actual Webdriver objects don't act same way across browsers. Ie my understanding is this - when you do send keys, you are sending it to ` **b** ' element which is a webdriver element. They dont respond uniformly across browsers for methods which they don't directly inherit under webdriver ' **ELEMENT** ` class like click etc. for this case what I am trying to say is when you do down on b, one down is good. but the next down will be on element b, but the cursor is not exactly focused on the element b as of now. – Makjb lh Feb 21 '16 at 03:10
  • So what I do is send Desktop or System keys - in Ruby I do something like this [link][http://stackoverflow.com/questions/6908662/sending-ctrlc-using-sendkeys-in-ruby] This will send the keys directly to system without some kind of focus on a single object - they are generic. So it will always work, no matter which browser of even a non browser GUI application. Ex: I have a common function where I simply pass the keys, after I click a drop down. that will do the job. In Java you can use robot class [link][http://stackoverflow.com/questions/22160704/java-robot-class-using-variables] – Makjb lh Feb 21 '16 at 03:14
  • Can you please show ur code in Java.Also,others are able to do this in Java using the same code.I thought I may have missed something..Can u tell me what robot.keypress(PA) does?? – dheeraj kateja Feb 21 '16 at 04:22

0 Answers0