HOW TO USE JavascriptExecutor in Microsoft edge using java and selenium - selenium

We have automation project we need to click menu items we use this below code
((JavascriptExecutor) test.getDriver()).executeScript("mCLICK(arguments[0]);", substr);
its is working well in chrome browser we want migrate to edge . we are running the same code in
micro soft Edge browser it's giving the below error
Exception class:org.openqa.selenium.JavascriptException
the reason is:org.openqa.selenium.JavascriptException: javascript error: Function is not a constructor.
Pls advise

I found the answer for click event for edge browser i put condition based on the browser
if(!browser.equals("edge")) {
String substr = child.getAttribute("substr");
((JavascriptExecutor)
test.getDriver()).executeScript("mCLICK(arguments[0]);", substr);
}
else
{
test.getDriver().findElement(By.name(items[0])).click();
Thread.sleep(2000);
test.getDriver().findElement(By.name(items[1])).click();
Thread.sleep(3000);
test.getDriver().findElement(By.name(items[2])).click();
}

Related

WebElement click() not working in Selenium

I was learning automated selenium testing in https://www.yatra.com/etw-desktop/. While trying to click an image button named 'Asia'(image is attached) ,I am getting a Time out exception. Please help me in figuring out what's going wrong .
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable
(By.xpath("//*[#id=\"scrollable1\"]/div[1]/div/a[2]/div[4]"))).click();
Thread.sleep(4000);
Assert.assertEquals("https://www.yatra.com/etw-desktop/city-list",driver.getCurrentUrl());
image showing the web element to be clicked
Try this:
How to Find Web Elements in Shadow DOMs using Selenium
It is most probably because of using Thread.sleep(), which is the worst case of explicit wait. Instead use the wait method provided by selenium itself.

NoSuchWindowException - Selenium 3.4.0 - IE 11

I have launched IE 11 browser,
I have navigated to a initial URL --> done mouse over and clicked a link --> it redirects to another page.
In that page, I have to click a button, but it is throwing an exception
org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window
but the windows still available on screen.
This is my code
WebElement e = driver.findElement(By.xpath("html/body/div[2]/div/div/header/nav/ul/li[2]/a"));
Actions a = new Actions(driver);
a.moveToElement(e).build().perform();
driver.findElement(By.xpath("//*[#id='menu-item-35']/a")).click();
TimeUnit.SECONDS.sleep(5);
// Exception is occurs after this, but when I delete the below code, the test case passes
driver.findElement(By.xpath("//*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input")).click();
This is the URL of the page: http://store.demoqa.com/
It looks to me like this is a race-condition error. I had those cases myself where I could actually see a window, but Selenium still said it wasn't there.
Have you tried setting the sleep time to a higher value?
You could also try to put a expect-wait-condition before clicking your element.
WebDriverWait wait = new WebDriverWait(DRIVER, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input"))
Here is the Solution to your Question:
A few words about the solution:
The xpath html/body/div[2]/div/div/header/nav/ul/li[2]/a looks petty vulnerable to me use linkText locator.
Once you use Action Class to build().perform(), induce a bit of wait before locating another element.
Instead of xpath locator of //*[#id='menu-item-35']/a element, use linkText locator.
Again the xpath //*[#id='default_products_page_container']/div[3]/div[2]/form/div[2]/div[1]/span/input looks petty vulnerable to me use a logical uniquexpath.
Here is your own working code block with some simple tweaks in it:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement e = driver.findElement(By.linkText("Product Category"));
Actions a = new Actions(driver);
a.moveToElement(e).build().perform();
driver.findElement(By.linkText("iMacs")).click();
driver.findElement(By.xpath("//input[#name='Buy']")).click();
Let me know if this Answers your Question.

Gmail logout functionality in selenium web driver

I am a beginner in selenium webdrive and try to make a code for gmail login and logout but not able to logout via "Id", it would be helpful for me if anyone suggest that how to logout it. Below are the code which I did please check and suggest:
public class seleniumExample {
public static WebDriver driver;
public static WebElement element, element1;
public static void main(String[] args) {
// TODO Auto-generated method stub
// Intialize chrome driver
driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
element = driver.findElement(By.id("Email"));
element.sendKeys("xyz#gmail.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
element1 = driver.findElement(By.id("Passwd"));
element1.sendKeys("xyz");
element.submit();
driver.findElement(By.id("gbi4m1")).click();
driver.findElement(By.id("gb_71")).click();
}
}
The following id :gbi4m1 does not point to anything. This is causing your test case to fail. As i said before google is not a right place to learn selenium, the page is complex and dynamic id's are hard to deal with if you are beginner.
Normally in browsers you can use development tools to check if a selector can be used to locate an element. So before you actually use a particular selector in your code, you can verify if it works. This will save a lot of time.
One more advantage of using this would be to check the uniqueness of a selector. If there are multiple elements with the same selector,findElement() returns the first one. In this
case the selector would be valid but it won't be unique, which in turn causes your test case to fail.
If a selector is unique, console displays only one element.
Let me explain with a simple example :
To click on Ask Question button on the top right of this page, following selector would work.
css = #nav-askquestion
So now you can confirm this by using the dev tools of your browser.
Just type $('selector_to_check') on the console and it displays all elements related with this selector.
I have used Chrome :
If the selector entered is invalid,an empty array is displayed.
First, id that you are taking doesn't exists. So that won't work. Secondly, once you correctly identify the logout button,it would be a bit more easy for us to identify exact nature of your problem
I'm not sure where did you get "gbi4m1"? anyhow why not use By.className
driver.findElement(By.className("gb_O")).click();
driver.findElement(By.id("gb_71")).click();
Seems to work for me
use cssSelector, Following code work for me
//Click on the profile image present in the right top corner
driver.findElement(By.cssSelector("span.gb_3a.gbii")).click();
//Click on 'Sign Out' button
driver.findElement(By.id("gb_71")).click(); //Close the browser
window driver.close();
this is awesome code for gmail logout in selenium webdriver
driver.findElement(
By.xpath("//*[#id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a/span"))
.click();
driver.findElement(By.id("gb_71")).click();
The following code is working for me :-
driver.findElement(
By.xpath("//*[#id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a/span"))
.click();
driver.findElement(By.id("gb_71")).click();
I used xpath for finding the account icon and it is working fine for me.

Mouse doubleclick not work in textbox using Selenium WebDriver

I try to double click on the text box by which the text will be selected.....The code looks like
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
WebElement txtBoxElement=driver.findElement(By.xpath("//*[#id='email']"));
txtBoxElement.sendKeys("abc");
System.out.println("Test start");
Actions builder=new Actions(driver);
Action a=builder.moveToElement(txtBoxElement).doubleClick(txtBoxElement).build();
a.perform();
//This is for another way to double click on the text field
Locatable locatable = (Locatable) driver.findElement(By.name("email"));
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(locatable.getCoordinates());
mouse.doubleClick(locatable.getCoordinates());
System.out.println("Test Complete");
}
but both of this way was not working I bypass this problem using this
Action a=builder.moveToElement(txtBoxElement).sendKeys(txtBoxElement,Keys.chord(Keys.CONTROL,"a")).build();
a.perform();
my question is why double click is not working?
I also try this for google search textbox it was also not working.
Here I want to mention one thing that I use Selenium 2.37 and firefox 26 .
I did not get any error but not double click on this element.I also observe that if I commented out the txtBoxElement.sendKeys("abc"); part and then sendkeys using Actions event,it write the text on browser address bar.
I believe you must be seeing following error while double clicking an element:
Cannot perform native interaction: Could not load native events component.
This is always issue with latest version of Firefox with latest verion of Selenium WebDriver(For more details please refer to link).
You can resolve this issue by downgrading Firefox from 26 to 25. With Firefox 25 your code will work.
It may be that you have the Action statement incorrect for the double-click. I did a double-click with simple statements like this:
Actions a = new Actions(driver);<br>
a.doubleClick(txtBoxElement);<br>
a.perform();
If you are trying to select all of the text in the text box try:
WaitUntil.elementReady(driver, 2,".//span[#class='scale-input-box']/input").sendKeys(Keys.CONTROL + "a");
To overcome problems with doubleclicking in Selenium try Alternative workaround Source
Simplified to this:
((JavascriptExecutor) driver).executeScript("document.getElementById('map_container').dispatchEvent(new Event('dblclick'));");

Selenium 2.0 / WebDriver clickAt() method unsupported

Selenium clickAt() function is throwing "Unsupported" exception while using with WebDriver (WebDriverBackedSelenium or just Selenium 2.x using ChromeDriver).
Is there any way to use this Selenium function via WebDriver?
Adding some code for context ...
ChromeDriver driver = new ChromeDriver();
driver.findElement(By.id("someID")).clickAt("25, 25");
.clickAt() method isn't even recognized ... however, using the WebDriverBackedSelenium is what provides the Unhandled exception.
You have to use Advanced User Interactions API
Click at the specific point inside an element looks like following:
ActionChainsGenerator builder = ((HasInputDevices) driver).actionsBuilder();
Action action = builder
.moveToElement(elementLocator, xOffset, yOffset)
.click()
.build();
action.perform();
At the moment, it is implemented for HtmlUnitDriver and InternetExplorerDriver only, other drivers are work in progress.
I have sometimes had similar problem and have fired the two MouseDownAt & MouseUpAt to solve the issue.. Seems as some JavaScript don't fire ok with clickAt always
Before you use click command on locator. you should use mouseOver on it.
Normally. this problem happen when link that need to click hidden or invisable.