Selenium webdriver automation, automating context menu options - selenium-webdriver

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 :)

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

Selenium Web-Driver Popup

I am unable to write on this popup message using selenium.
Please feel free to help me in this case.
My Code is:-
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\gecko_driver\\geckodriver-
v0.16.1-win64\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("https://www.heycare.com");
driver.findElement(By.xpath("html/body/div[2]/header/div/nav/div/a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("hello world-----1");
driver.switchTo().frame(0);
driver.findElement(By.id("mobile")).sendKeys("7015273543");
System.out.println("hello world-----2");
//Driver.findElement(By.id("mobile")).sendKeys("7015273543");
driver.findElement(By.id("Pass")).sendKeys("123456");
}
Error:-
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: *[name='mobile']
Code is working fine till:-
driver.findElement(By.xpath("html/body/div[2]/header/div/nav/div/a")).click();
Unable to write on that popup form that comes after clicking on the login button..
I navigated to that website and successfully targetted the mobile number field using this css selector #mobile which targets the id attribute. You're doing this for your password field. Try having your test use this:
Driver.findElement(By.id("mobile")).sendKeys("7015273543");
See if that helps...
Edit: I'm not sure that the unformatted code block you're showing that switches frames is necessary. Unless you're working with iframes, I didn't see any in the minute I was looking around.
Edit2:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.heycare.com");
System.out.println("hello world");
driver.findElement(By.className("log-pop")).click();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.name("mobile")));
driver.findElement(By.name("mobile")).sendKeys("7015273543");
driver.findElement(By.id("Pass")).sendKeys("123456");
You could try something like this, I added in a call to WebDriverWait to account for the possible delay between clicking the login button, and waiting for the login prompt popup to finish it's animation prior to be allowed to be clicked. I'm not sure if this will work in your case, but it's a possibility I sniffed out when I was observing the site's behavior.
(I didn't test any of this code, it's freehand written. So take it with a grain of salt. It might not work flawlessly as is.
Bit of java nit, you should make your object references lowercase, save capitalized symbols for class names. I had a moment of trying to figure out why you were calling static methods of a class called Driver instead of an instance of the class WebDriver

Unable to automate the Popup in selenium

i am unable to identify the web elements when a pop up is displayed on clicking "select your family members" in the below website, i am not sure how to automate this ?
I have tried using Switch window and alert windows.
http://health.policybazaar.com/?utm_content=home_v3
There is no need to switch any window , Use following code
driver.get("http://health.policybazaar.com/?utm_content=home_v3");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.id("input_6")).click();
Updated :
WebDriverWait wait =new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='prequote-member-left-section']/md-checkbox[#name='checkboxSelf']/div[1]")));
driver.findElement(By.xpath("//div[#class='prequote-member-left-section']/md-checkbox[#name='checkboxSelf']/div[1]")).click();
Note : Use ExplicitWait to make element visible on next popup and then click

moveByOffset().contextClick() clicks outside the WebElement

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

After switching to new tab not able to locate the element

In wordpress site,I am opening a link in a new tab to add a new post, I am able to enter the post title but not able to click on publish button. I have tried all possible locators id, xpath...etc.
can someone help me understand this issue. This screenshot shows page after i open link in new tab.
Here is my sample code:
driver.get("http://localhost/wordpress/wp-login.php");
driver.manage().window().maximize();
driver.findElement(By.id("user_login")).sendKeys("admin");
driver.findElement(By.id("user_pass")).sendKeys("q1w2e3r4");
driver.findElement(By.id("wp-submit")).click();
Actions actions = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='welcome-panel']/div/div/div[2]/ul/li[1]/a")));
actions.keyDown(Keys.CONTROL).perform();
actions.keyDown(Keys.SHIFT).perform();
driver.findElement(By.xpath(".//*[#id='welcome-panel']/div/div/div[2]/ul/li[1]/a")).click();
actions.keyUp(Keys.SHIFT);
actions.keyUp(Keys.CONTROL);
driver.findElement(By.id("title")).sendKeys("TestPost"); //Working
driver.findElement(By.id("publish")).click(); //not working
Thanks in advance

Resources