javascript for mouse hover in c# selenium - javascript

I wanted to mouse hover on a menu which has many sub menu. In the websites where I researched they suggest below actions to use. But actions hovers over other menu there by hiding the actual element that has to be hovered.
Actions action = new Actions(Driver);
Actions hoverclick = action.MoveToElement(HomePageMaps.MegaMenuDevelopAndGrowAsManager());
hoverclick.Build().Perform();
Please suggest a java script for mouse hovering that can be used in selenium C# [Visual Studio IDE].
As well I have tried the below java script for hovering but it doesn't hover instead it just brings the focus on the element.
IJavaScriptExecutor exe = (IJavaScriptExecutor)Driver;
exe.ExecuteScript("arguments[0].fireEvent('onmouseover');", xpath of the element to be hovered());

You could try and make use of the move to element action, this will simulate the action.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id(elementId)));
Actions action = new Actions(driver);
action.MoveToElement(element).Perform();

Try using JQuery,
IJavaScriptExecutor exe = (IJavaScriptExecutor)Driver;
exe.ExecuteScript($("Your Element Selector").hover(function(){$(this).css("background-color", "white"); },);
Your Element Selector - Give your Web Element which is to be hovered.

Related

Button was not actually clicked but shows element.click() performed in selenium script

I'm writing an automation script for a Pega Web application. I have button click functionality, but actually the button is not getting clicked when i ran the script.When I check the logs, it shows that action was performed but it's not actually clicking the button.
I tried below actions but nothing works,
WebDriverWait wait = new WebDriverWait(driver, 80);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Submit')]")));
driver.findElement(By.xpath("//button[contains(text(),'Submit')]")).click();
Then i tried below code,
WebDriverWait wait = new WebDriverWait(driver, 80);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Submit')]")));
WebElement button=driver.findElement(By.xpath("//button[contains(text(),'Submit')]"));
button.sendKeys(Keys.RETURN);
Also i tried javascript as well,
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", button);
I have faced similar issue multiple times, I fix it using -
while(driver.findElements(By.xpath("//button[contains(text(),'Submit')]")).size()!=0)
{
driver.findElement(By.xpath("//button[contains(text(),'Submit')]")).click();
}
This is just a temporary fix, I found it somewhere on Internet only. I believe it's some issue with the driver itself.
---- Edit ----
Before clicking on the button, can you make sure if the page is completely loaded(Ajax) by using below javascript commands-
jQuery.active ==0 && document.readyState == "complete"

Clicking an image button using selenium webdriver after login

I'm trying to automate a login page using selenium-webdriver and I'm using IE as the browser. The problem that I'm experiencing is that after the login I'm not able to select the image button. I have even introduced time delay after the login credentials are entered still the script runs and the button doesn't gets clicked. P.S I have commented all the methods I have tried too.
The code I have used is:
System.setProperty("webdriver.ie.driver",
"C:/Program Files/IEDriverServer/IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
driver.manage().window().maximize();
driver.navigate().to("website name");
driver.findElement(By.id("userid")).sendKeys("username");
driver.findElement(By.id("password")).sendKeys("password");
//driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("submitButton")).click();
Thread.sleep(5000);
//driver.findElement(By.id("imgBtnAdd")).click();
//driver.findElement(By.cssSelector("a[href='Images/Go.gif']")).click();
driver.findElement(By.xpath("//img[# src='Images/Go.gif']")).click();
//System.out.println("Manual Click of Ok button");
//By xpath = By.xpath("//button[#name='imgBtnAdd'][#type='image'][contains(image(),'Images/Go.gif')]");
//WebElement myDynamicElement = (new WebDriverWait(driver, 10))
//.until(ExpectedConditions.presenceOfElementLocated(xpath));
//myDynamicElement.click();
driver.findElement(By.cssSelector("input[id='rblRoleGroup_4']")).click();
driver.findElement(By.id("imgBtnRoleGroup")).click();
//WebElement element = driver.findElement(By.id("imgBtnAdd"));
//Thread.sleep(5000);
//JavascriptExecutor executor = (JavascriptExecutor)driver;
//executor.executeScript("arguments[0].click();", element);
//driver.close();
You can try sendkkeys :
driver.findElement(By.id("imgBtnAdd")).sendKeys(Keys.ENTER);
If image is in new window try this:
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
Try click using JavascriptExecuter might help you -
WebElement element = driver.findElement(By.id("imgBtnAdd"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
After viewing your code, I have noticed that your xpath was wrong.
Instead of using <img> tag, use <input> tag.
driver.findElement(By.xpath("//input[#src='Images/Go.gif']")).click();
Explanation of xpath:- Use src attribute of <input> tag.

Close Flipkart open pop-up and go to main window using Selenium

WebDriver driver = new FirefoxDriver();
driver.get("https://www.flipkart.com");
driver.manage().window().maximize();
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler);
I tried it by switching to main window also. Please add valuable input or code to close the pop up.
You can try using the java Robot API by importing java.awt.Robot libraries. An example is here:
One solution for File Upload using Java Robot API with Selenium WebDriver by Java
You can try to use it similarly to press the Esc key. Pressing Esc on flipkart website gets rid of the pop-up.
The pop-up which appears on Flipkart's website is a simple HTML modal. Window handle is used when a new pop-up window needs to be accessed.
To close the pop-up just click on the cross on the top right corner of the pop-up. Use waits to ensure that selenium finds the WebElement.
Try this:
driver.get("https://www.flipkart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement cross = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.className("close-icon")));
cross.click()

Unable to click ViewSeats button in redbus site using selenium

I was trying to automate a flow using redbus site. I was not able to click View Seats button in the second page after selecting journey details in the first page of RedBus site. I have tried clicking using javascript click() as well.
Please find the code below:
WebDriver ffdri = new FirefoxDriver();
ffdri.get("https://www.redbus.in/Booking/SelectBus.aspx? fromCityId=123&toCityId=122&doj=10-Oct-2015&busType=Any&opId=0");
ffdri.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(ffdri,20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Travels")));
JavascriptExecutor js = (JavascriptExecutor) ffdri;
js.executeScript("document.getElementsByClassName('dpBtn')[1].click();");
ffdri.findElement(By.id("BusType_AC")).click();
ffdri.findElement(By.id("BusType_Sleeper")).click();
js.executeScript("document.getElementsByClassName('dpBtn')[1].click();");
ffdri.findElement(By.linkText("Fare")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[#class='viewSeatsBtn']"))).click();
// System.out.println(val.getText());
//js.executeScript("document.getElementsByClassName('viewSeatsBtn')[0].click();");
// WebElement ViewSeats = ffdri.findElement(By.xpath("//button[#class='viewSeatsBtn']"));
//js.executeScript("arguments[0].click();", ViewSeats);

Using JavascriptExecutor to sendKeys plus click on web element

I'm trying to open a link in a new tab, then switch to that tab, in a Firefox browser, using selenium in Java. It's my understanding that in order to do this, I need to use a send keys combination.
In order to open the link in the same window, I've been using something like this:
WebElement we = driver.findElement(By.xpath("//*[#id='btn']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", we);
The above was working fine for me.
Now I'm trying to also sendKeys, as in below, which is not working:
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("keyDown(Keys.CONTROL)
.keyDown(Keys.SHIFT)
.click(arguments[0])
.keyUp(Keys.CONTROL)
.keyUp(Keys.SHIFT);", we);
Any advice? I can't figure out the correct syntax to sendKeys to JavascriptExecutor. I've seen some similar solutions using Actions, but this hasn't worked for me either.
try below code to open any link on page to new tab & switch to that tab. Perform operations there & go back to first tab for further execution.
WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com/");
WebElement e = driver.findElement(By.xpath(".//*[#id='nav-questions']"));
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).build().perform(); //press control key
e.click();
Thread.sleep(10000); // wait till your page loads in new tab
action.keyUp(Keys.CONTROL).build().perform(); //release control key
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); //move to new tab
driver.navigate().refresh(); // refresh page
driver.findElement(By.xpath(".//*[#id='hlogo']/a")).click(); //perform any action in new tab. I am just clicking logo
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); //switch to first tab
driver.navigate().refresh();
driver.findElement(By.xpath(".//*[#id='hlogo']/a")).click();// refresh first tab & continue with your further work.I am just clicking logo

Categories