Alternative to contextClick in Chrome: Selenium-WebDriver- Java - java

I know one can find similar question,but no one gave an alternate solution.Please bear with me.
After some research I realized it is not possible to automate contextClick in Chrome browser.For ex:
If I need to perform below code and browser has to be Chrome-
driver.get("https://www.google.com");
Actions ac= new Actions(driver);
ac.moveToElement(driver.findElement(By.id("hplogo"))).contextClick().sendKeys(Keys.ARROW_DOWN).build().perform();
It would be helpful if I can get an alternative to using contextClick options.
Thanks

On my chrome browser, contextClick is working fine. Try below line of code, it may resolve your problem.
ac.moveToElement(driver.findElement(By.id("hplogo"))).contextClick().sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();

Are you trying to do context click only, then
Actions conClick=new Actions(driver);
//i am expecting you are try to perform right click id = hplogo
conClick.contextClick(driver.findElement(By.id("hplogo"))).build().perform();
// if you want to select or click any option in this context menu
// go for click with specific location, no need of keys
// if required use sleep before click
driver.findElement(By.id("required location")).click();
if above click after context click is not working as expected, then you can also try below way
Actions conClick1=new Actions(driver);
//i am expecting you are try to perform right click id = hplogo
//after context click moving to 25 vertically, may be second option in context menu and clicking
conClick1.contextClick(driver.findElement(By.id("hplogo"))).moveByOffset(5, 25).click().build().perform();
Thank You,
Murali

Related

unable to launch new tab from right click action | Java Selenium

I'm trying to right-click on a link and use the keys arrow-down and enter to open a new tab.
Problem: No error displayed during execution. The link is identified and a right-click is made on it. however, the code is not performing the down-arrow and entering the action. Can't seem to figure out why.
Can somebody help?
Code:
a.moveToElement(subfooterdriver.findElements(By.tagName("a")).get(1)).contextClick().sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
a - obj to action class,
subfooterdriver - is a frame consisting of 5 links
Updated:
I was able to overcome the issue with the following code but I want to understand what went wrong with the first code.
String clickonlink=Keys.chord(Keys.COMMAND,Keys.ENTER);
subfooterdriver.findElements(By.tagName("a")).get(i).sendKeys(clickonlink);
You can get the link inside this hyper text then open it in new tab as following :
//element locatore
WebElement link =driver.findElement(By.tagName("a"));
//Get Required URL
String url = link.getAttribute("href");

Selenium <> AgGrid Multiple Arrow Down using Action class

I am using Selenium with AgGrid (https://www.ag-grid.com/example.php)
I am trying to select the In Range option from the Total Winnings drop-down using the code below, I was wondering if there is a better way to repeat the sendKeys(Keys.ARROW_DOWN) instead of mentioning it again and again in a chain i.e. something like sendKeys(Keys.ARROW_DOWN).perform(6) (to be honest I didn't find anything in the API).
System.out.println("Select the Column");
driver.findElement(By.xpath("//div[#class='ag-header-cell ag-focus-managed'][#aria-colindex='10']//span")).click();
driver.findElement(By.xpath("//div[#class='ag-wrapper ag-picker-field-wrapper']")).click();
Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN)
.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
The above code is working and does perform the action however, just wondering if there's a better way to code this.
When the popup is rendered, you can rely on the role attribute of that popup, which is 'option'.
You can use this XPath query to select options inside the filter options popup:
let selectOptionQuery = '//div[#role="option" and contains(.,"In range")]';
I've created a simple example which selects the 'In range' option at the click of a button: https://plnkr.co/edit/S7A4EguRWmTjjl7g?preview

Problem clicking elements using class and index

I'm trying to write code to automatically DM people on Instagram. Im stuck on getting the code to click on the DM button.
In UIautomatorviewer, there is no text or a resource-id, so I tried using the class.
When I run the code it doesn't click on the right thing. It clicks on the button NEXT to it.
Here is the code -
By path2 = By.xpath("//android.widget.ImageView[#index='3']");
driver.findElement(path2).click();
Thread.sleep(5000);
Can anyone help? I'm new to this so i'm not very experienced.
Because I can not see the entire XML I can not really tell what you did wrong but probably your xpath is not correct.
With UIAutomatorViewer you can save the XML and then you can test your xpath on it. Either with XMLSpy or an online tool like https://www.freeformatter.com/xpath-tester.html.
Probably there are more android.widget.ImageView with index = 3 and I think appium selects the first one? So you could change your xpath to a more unique one like:
//android.widget.FrameLayout//android.widget.LinearLayout//android.widget.ImageView[#index='3']"
Based on UIAutomator Viewer screen you can also use content-desc as shown below
By path2 = By.xpath("//android.widget.ImageView[contains(#content-desc,'Message')]");
driver.findElement(path2).click();

Press ENTER key in Selenium WebDriver with java when element property not present

I am using selenium webdriver with Java to automate the webpages
When I enter the url, I am getting the authentication required dialog box
I am able to enter the username and password by configuring profile
but I am not able to click on OK button
Note: Not able to get the ok button property so am not able to
use the below code
import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);
Is there any other way to press on ok button through webdriver?
You need to handle it as an alert box,
wait for popup to appear and click OK.
Below code waits up to a maximum of 10 seconds for the popup to be present and then accepts it by clicking OK. Although wait is optional.
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Handling credential boxes is not possible directly using Selenium You can use the JAVA AWT robot class to press enter. This class is available in the java API itself.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
Alternatively, you can use AutoIt or an image based testing tool like SIKULI http://www.sikuli.org.
Please note that when you are using these solutions, the workstation screen cannot be locked while running the test cases.
Try this code snippet:
driver.findElement(By.xpath("//body")).sendKeys(Keys.RETURN);
It will definitely work.

How to click on a button with force using java selenium web driver

I am using selenium web driver with java.
And developing automation for my app in facebook, so whenever I add my app for first time in facebook, it is asking for permissions with okay button
I am trying to click on okay button with my code but that is not working.
Is there a better way to click on okay button using selenium web driver with java?
What I tried is:
1)driver.findElemenr(element).click();
2)Actions action = new Actions(driver);
action.click(element)
3)Actions action = new Actions(driver);
action.moveToElement(element).click()
4)Actions action = new Actions(driver);
action.KeyDown(element, Keys.ENTER);
Please let me know the reason before if you want to downvote my question
I think you are looking for isEnabled() method in Selenium. What you can do is, after you click the button using webdriver you can check the status of the button and repeat the click process if the button is still enabled.
You just try with logic something like below.
int i=0;
while(isElementPresent(button) && i<10)
{
Thread.sleep(1000);
driver.findElement(button).click();
i++;
}
The above code will try to click on button until it present or i (int i)reached 10. (loop breaking point)
You can find isElementPresent method implementation here.
you can try this,
WebDriverWait button = new WebDriverWait(driver,60);
button.until(ExpectedConditions.elementToBeClickable(element));
button.click();
This will wait 60 secs for the button to be clickable,if condition (element is clickable) is met before 60 secs, well and good, button will be clicked, else an exception will be thrown...
ExpectedConditions class provides many useful methods.

Categories

Resources