Right click (Context Click) not working - selenium

I have made a number of tests that I run via selenium. However I'm having problem with right click on an element. I get an exception saying that an element could not be located on the page using the given search parameters.
This is strange because just before I right click on it I select the element using a click event. I'm unsure why I get the exception when I'm trying to right click on an element I'm able to select.
My code is as follows:
var e= webDriverWait
.Until(ExpectedConditions
.ElementTo‌​BeClickable(session.FindElementByName(elementName))
);
webDriverWait
.Until(ExpectedConditions.ElementToBeClickable(e))
.Click();
Actions action = new Actions(session).ContextClick(e);
action
.Build()
.Perform();

Related

Cant insert text - a msg that the element is not visible is displayed

I run automation on a site that after I click on a button and a screen of PayPal is opened for inserting details. The PayPal is opened in another tab. I added a syntax that move the testing to the relevant tab and then I insert a syntax that checks that the "email input" field exists (to check that it is really goes to the correct tab) - and the result of this test :- field exists.
Then - I add a syntax for the same field to insert the email and the test is failed - the text is not inserted and there is a msg that the field is not visible.
No need to do scroll because the filed is in the top of the screen.
What can I do in this case?
This is the relevant code:
String oldTab = driver.getWindowHandle();
comOps.clickOrChose(PLS.buyButton);
Thread.sleep(4000);
ArrayList<String> newTab = new ArrayList<String> (driver.getWindowHandles());
newTab.remove(oldTab);
driver.switchTo().window(newTab.get(0));
comOps.verifyElementExist(PLP.payPalEmail);
comOps.insertText(PLP.payPalEmail, "paypal-buyer#makeitleo.com");
The reason you get this error message is that the element apparently exists but is not visible when you try to enter text to it. There are a lot of possible reasons why the element is not visible.
Given that a new tab is opended a probable reason is that the page (and its elements) is still loading. If this is the case, you need to wait for the visiblity of the element, e.g. using this piece of code (before inserting text):
WebDriverWait wait = new WebDriverWait(driver, 300); //waiting up to 5 minutes
ExpectedCondition<WebElement> condition =
ExpectedConditions.visibilityOf(PLP.payPalEmail);
wait.until(condition);
Note: that this solution assumes that PLP.payPalEmail is of type org.openqa.selenium.WebElement. If it is of type org.openqa.selenium.By use visibilityOfElementLocated(By locator).
Telling from your code snippet, I assume that comOps is an object of a class wrapping all Selenium actions. So, it is a good idea to place the above code in some method inside that class which could look like this:
public void verifyElementVisible(WebElement element) {
WebDriverWait wait = new WebDriverWait(driver, 300); //ToDo: use configurable timeout
ExpectedCondition<WebElement> condition =
ExpectedConditions.visibilityOf(element);
wait.until(condition);
}
and call it like this
comOps.verifyElementVisible(PLP.payPalEmail);

<Select> Tag is not working for selecting value in dropdown

I am facing an issue where dropdown has tag. but still I am unable to select value in dropdown and it is throwing exception. I am able to get dropdown values but unable to select
Here is complete details
URL : https://semantic-ui.com/modules/dropdown.html
Testcase: Select multiple values in Skill dropdown.( Find attachment for exact field on web page.
WebDriver driver = new FirefoxDriver();
driver.get("https://semantic-ui.com/modules/dropdown.html");
driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
WebElement Dropdown = driver.findElement(By.name("skills"));
Select sel = new Select(driver.findElement(By.name("skills")));
List<WebElement> Options = sel.getOptions();
System.out.println(Options.size());
for(int i=0;i<Options.size()-1;i++) {
driver.findElement(By.xpath("//*[#id=\"example\"]/div[4]/div[1]/div[2]/div[4]/div[1]/div[8]/div")).click();
System.out.println(Options.get(i).getAttribute("value"));
if(Options.get(i).getAttribute("value").equalsIgnoreCase("angular")||Options.get(i).getAttribute("value").equalsIgnoreCase("Graphic Design")||Options.get(i).getAttribute("value").equalsIgnoreCase("HTML")) {
Thread.sleep(6000);
sel.selectByIndex(i);
}
}
}
Exception:
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException:
Please help to suggest for this.
Your drop down is a simulated drop down by css, not a HTML native drop down: Select. So you can not operate it as native drop down.
After look into the HTML code of your dropdown, there is an embed native drop down, but it's always invisible no matter you expand options or not. Selenium can't operate on invisible element(But you can read value/attribute from it), that's why the exception you met.
Actually all options come from the div class="menu", so you should click the option from div class="menu" as below screenshot show:
Code to resolve your problem:
// click arrow down to expand options
driver.findElement(By.cssSelector("select[name='skills'] + i")).click();
// choose option: Angular
driver.findElement(By.xpath("//div[contains(#class, 'multiple')][select[#name='skills']]//div[.='Angular']"));

Selenium click sometimes not working using Edge driver

Please see pic below. I have tried to select the input tag through the following method using Selenium:
1. `element.Click();`
2. `element.SendKeys(Keys.Enter);`
3. Actions action = new Actions(Browser.Driver).MoveToElement(element);
action.Click().Perform();
I am trying to click the checkbox by returning it through a property in code that has the following code:
WebDriverWait wait = new WebDriverWait(Browser.Driver, TimeSpan.FromSeconds(60));
IWebElement checkbox = wait.Until(ExpectedConditions.ElementExists((By.CssSelector("label[for='PerformerIndependence_AcceptTermsAndConditions']"))));
return checkbox;
or
WebDriverWait wait = new WebDriverWait(Browser.Driver, TimeSpan.FromSeconds(60));
IWebElement checkbox = wait.Until(ExpectedConditions.ElementExists((By.CssSelector("input#PerformerIndependence_AcceptTermsAndConditions"))));
return checkbox;
For some reason If I run the code in debug mode to test the element returned to click it will click. If I just run my test in run mode using Visual Studio2015, the checkbox is not clicked. The method using the element to click for the three attempts is wrapped in a try/catch block. Please help!!!
Screenshot
I am also facing this issue. Click on checkbox is not working in Edge..
There are two work around of this:
click using JS
use action class..
WebElement element=driver.findElement(By.id("abc"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
==========================================================
Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
Both worked fine in my case.

Selenium WebDriver Log Out from Facebook

Does anyone know how to click log out button? I tried using driver.findElement(By.linkText("Log out"));
But it returned an error saying element is not found. Is it because the list is dynamically generated?
You should try using WebDriverWait to wait until elementToBeClickable it works for me as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement accountSettings = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Account Settings")));
accountSettings.click() //this will click on setting link to open menu
WebElement logOut = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log Out")));
logOut.click() // this will click on logout link
Hope it helps...:)
I assume, after click on arrow button, Log Out button appers in ur code. So to click on that log Out button, use the below portion as cssSelector:
a[data-gt*='menu_logout']>span>span._54nh
driver.findElement(By.cssSelector("a[data-gt*='menu_logout']>span>span._54nh"));

On right click(or contextClick) going out of iframe. How to perform correctly?

In my app, i need to perform right click action on one element in a frame. I switched to that frame and the normal click action performs correctly, but for right click, the action is performing somewhere out side of the frame but not on the specified element.
To cross check I used following code:
//switch to iframe
driver.switchTo().frame(driver.findElement(By.xpath("//tr[2]/td/iframe")));
//click on element
driver.findElement(By.xpath("//My xpath")).click();//correctly clicked on element
WebElement span=driver.findElement(By.xpath("//My xpath"));
//right click
Actions right=new Actions(driver);
right.contextClick(span).build().perform();//not clicked on element. context click performed out side of frame
Try applying sleep between the element i.e.
driver.findElement(By.xpath("//My xpath")).click();
Thread.Sleep(1000);
WebElement span=driver.findElement(By.xpath("//My xpath"));