moveByOffset().contextClick() clicks outside the WebElement - selenium-webdriver

I am very new to Selenium WebDriver and Programming. I am trying to right click on the link (http://docs.seleniumhq.org/download/2.48.2 link under Selenium Standalone Server)but code below right-clicks on the outside of the link and hence the contextual menu is not relevant. How can I make the right-click on the link above? I took the right xpath but still....
WebElement dom = obj.findElement(By.xpath("/html/body/div/div[2]/div[2]/p[3]/a"));
Thread.sleep(3000);
Actions act = new Actions(obj);
int x = dom.getLocation().getX();
int y = dom.getLocation().getY();
act.moveByOffset(x,y).contextClick().build().perform();
Thread.sleep(5000);
act.sendKeys(Keys.DOWN).build().perform();
Thread.sleep(2000);
act.sendKeys(Keys.DOWN).build().perform();
Thread.sleep(2000);
act.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
obj.quit();

If you want to right click on download link (xpath=.//*[#id='mainContent']/p[3]/a) then
Actions rightClick=new Actions(driver);
rightClick.contextClick(driver.findElement(By.xpath(".//*[#id='mainContent']/p[3]/a"))).build().perform();
Let me know how it works..
Thanks,
Murali

Related

How to write selenium code for clicking on the link and opening the link in new tab

Hi Please let me know how to
Write selenium code for right click on the link and open the link .I am aware that i can use keyboard functions but i am bit confused
try the following code ... this is the code for right clicking on "About" link which appears in google page.. u can open that in new tab by selecting options in context menu.....
WebDriver driver = new ChromeDriver();
driver.get("http:\\google.com");
WebElement ele= driver.findElement(By.linkText("About"));
Actions ac=new Actions(driver);
Thread.sleep(2000);
ac.contextClick().sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_UP).sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
driver.quit();

Hover Electronics -> Mobiles and Click using Selenium in Flipkart UI using Chrome

Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id=\"container\"]/div/div[2]/div/ul/li[1]/span"));
action.moveToElement(we).moveToElement(driver.findElement(By.xpath("//*[#id=\"container\"]/div/div[2]/div/ul/li[1]/ul/li/ul/li[1]/ul/li[1]/a[#href='/mobile-phones-store?otracker=nmenu_sub_Electronics_0_Mobiles']"))).click().build().perform();
I am trying to hover and click using xpath. The code is not throwing any error but it is still unable to perform click option after hover.
Try with the below code snippet. I have tested it with chrome driver and works fine.
WebElement electronics_menuname = driver.findElement(By.xpath("//span[contains(text(),'Electronics')]"));
Actions builder = new Actions(driver);
builder.moveToElement(electronics_menuname).build().perform();
WebElement mobile_menu_button = driver.findElement(By.xpath("//li[#class='_1KCOnI _2BfSTw _1h5QLb _3ZgIXy']//a[contains(text(),'Mobiles')]"));
new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(mobile_menu_button)).click();

Have to double click the buttons on a webpage while using selenium webdriver

I am using this code to click on on a button
driver.findElement(By.linkText("Sign up")).click();
I tried using:
new WebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(By.linkText("Sign up")))
but it did not work.
Try this sample as you does not mention URL:
WebDriver driver =new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://only-testing-blog.blogspot.in/2014/09/selectable.html");
WebElement ele = driver.findElement(By.xpath("//button[contains(.,'Double-Click Me To See Alert')]"));
//To generate double click action on "Double-Click Me To See Alert" button.
Actions action = new Actions(driver);
action.doubleClick(ele);
action.perform();
Go through this URL to learn more:
http://www.software-testing-tutorials-automation.com/2015/01/double-click-on-button-using-actions.html

Selenium webdriver (Double click on a search result displayed as label)

I am very new to selenium and need to double click on a label(appears after a successful search result).
Please help.
enter image description here
Attached is the page look like.
You should use the Actions() class as this includes a 'double-click' action.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//select[#id='groupselect']/#option[#value='942711']"))).doubleClick().build().perform();
OR
Actions action = new Actions(driver);
WebElement element=By.xpath("//select[#id='groupselect']/#option[#value='942711']"));
//Double click
action.doubleClick(element).perform();
//Mouse over
action.moveToElement(element).perform();
//Right Click
action.contextClick(element).perform();
Hope it will help you :)

Selenium webdriver automation, automating context menu options

I am not able to click on any element after doing right click in selenium(Java).
Its just doing a right click and is not clicking of any of the options like open in new tab...Instead its just doing a normal click after doing a right click.Can anyone please help me.Below is my code
System.setProperty("webdriver.chrome.driver","C:\Selenium\chromedriver.exe");
WebDriver wd=new ChromeDriver();
wd.get("http://google.com");
Thread.sleep(3000);
//Point a=wd.findElement(By.linkText("Testing")).getLocation();
WebElement b=wd.findElement(By.linkText("About"));
Actions action=new Actions(wd);
//action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
action.moveToElement(b);
Thread.sleep(4000);
//action.contextClick(b);
action.contextClick(b);
action.sendKeys(Keys.ARROW_DOWN).sendKe ys(Keys.ENTER).build().perform();
I have tried via context click and move to element as well but no result.Thanks in advance..
Might be this is what you want:
To select the item from the contextual menu, you have to just move your mouse positions with the use of Key down event like this:-
Actions action= new Actions(driver);
action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
Soure: Select an Option from the Right-Click Menu in Selenium Webdriver - Java
As per request in the comments adding alternative way to open a link in new tab.
System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
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).perform();
Thread.sleep(4000);
action.keyDown(Keys.CONTROL);
action.click();
action.keyDown(Keys.CONTROL).build().perform();
//action.sendKeys(Keys.RETURN).perform();
Thread.sleep(4000);
driver.quit();
The build() method is used compile all the listed actions into a single step. We use build() when we are performing sequence of operations. We can directly use perform() if we are performing single action. You can read more about Actions class.
Also, re-iterating that I was unable to find the root of the issue in the limited time I had, so I placed a work around. So like we use the shortcut CTRL+CLICK to open the link in new tab manually, You might need to find out the shortcut for what you need, you can refer this : https://support.google.com/chrome/answer/157179?hl=en
Hope it helps :)

Resources