Mouse hover on element - selenium

http://www.franchising.com/ ---> Mouse over on (Franchises A-Z) ---> need to click Q
I have tried with the following
WebElement we1=driver.findElement(By.cssSelector("a[href='/franchises/']"));
WebElement we2=driver.findElement(By.cssSelector("a[href='/franchises/q.html']"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((JavascriptExecutor) driver).executeScript(js, we2); // I have used the script since the we2 is not visible
Actions builder=new Actions(driver);
builder.moveToElement(we1).perform();
Thread.sleep(5000);
we2.click();
could any one try and share me the code... Still I'm getting "ElementNotVisibleException"

With firefoxdriver, a lot would depend on what version of driver you are using and what version of Firefox you have on your system since native support would differ based on that.
Following works on Chrome :
WebElement link1 = driver.findElementByLinkText("Franchises A-Z");
Actions action = new Actions(driver);
action.moveToElement(link1).click(driver.findElementByXPath("//a[contains(#href,'franchises/b')]")).perform();

Before going in to the code i just want to you to ensure the version of Selenium server you are using. Please make it to the updated version of 2.28.x
Code:
driver = new FirefoxDriver();
driver.get("http://www.franchising.com/franchises/");
Thread.sleep(5000);
WebElement element=driver.findElement(By.xpath("//tr[3]/td/table/tbody/tr/td[4]/a"));
Actions builder = new Actions(driver);
builder.moveToElement(element).build().perform();
Thread.sleep(5000);
it works fine for me. Try this code. I hope this will work.

Related

Selenium java webdriver 3: moveToElement not working

Selenium java webdriver 3: moveToElement not working.
WebElement element = ...
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
Tried, adding click()
WebElement element = ...
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
Not working. The mouse is not moved.
WebElement abcd = ..........
Actions actions = new Actions(driver);
actions.moveToElement(abcd).perform();
Above code will work, but please check with the chrome.exe version and the chrome version you are using it your machine. both should be compatible to one another. Check the compatibility with below link.
https://sites.google.com/a/chromium.org/chromedriver/
Skip the build() part, perform() does it underneath anyway.
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.linkText("host"));
actions.moveToElement(element).build().perform();
This will work. first check your "find element" method is write or wrong. Please post this step as well. otherwise your code is correct.
try to find element by xpath rather than link text. it worked for me.
WebElement element = driver.findElement(By.xpath("..."));
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
Try below code
public static void mouse_movement(WebDriver driver, String xpathExpression) {
Actions act = new Actions(driver);
WebElement target = driver.findElement(By.xpath(xpathExpression));
act.moveToElement(target).build().perform();
}
if you need to click the element you could try javascript:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.xpath(xPath)));
I have solved this problem with:
const element = await driver.findElement(...)
await driver.executeScript("arguments[0].scrollIntoView(true);", element)
await driver.sleep(500);

I want to perform right click using webdriver in Chrome ? Please Suggest

I want to perform right click using web driver in Chrome ? Please Suggest
Below is the code I am using:-
Actions actions = new Actions(driver);
Action action=actions.contextClick(element).build();
action.perform();
This works for me in c#
Actions actions = new Actions(Driver.Instance);
actions.ContextClick(element).Perform();
Actions action= new Actions(driver);
action.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
if you want to select the first option from the right click drop down.
Try the below code:
Actions act = new Actions(driver); // where driver is WebDriver type
act.moveToElement(webElement).perform();
act.contextClick().perform();

How to disable mouse right click in selenium in java

I have written an application in selenium using JAVA to login into website. Following is my code
import org.openqa.selenium.JavascriptExecutor;
WebDriver driver = new FirefoxDriver();
driver.get("http://inernalportal.com");
WebElement element = null;
element = driver.findElement(By.id("txtLoginID"));
element.sendKeys("user");
element = driver.findElement(By.id("txtpassID"));
element.sendKeys("password");
element = driver.findElement(By.id("btnLogin"));
element.click();
However above code works correctly but there is one issue, while filling password by Seleinum driver user could click in address bar to see password.
Is there any way to prevent left/right mouse click in Selenium?
Your help is highly appreciable.
That should resolve problem :)
((JavascriptExecutor) driver).executeScript("document.getElementById(\"txtpassID\").value = \"password\";");

Actions and htmlunitdriver - speed issue

My web application has menus that open on MouseOver. I'm writing tests using htmlunitdriver.
Test code to trigger menu is
Actions builder = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[starts-with(#href,'/index.html')]"));
Thread.sleep(2000);
builder.moveToElement(menu).build().perform();
Thread.sleep(2000);
driver.findElement(By.xpath("//a[starts-with(#href,'/submenuitem')]")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
When I run a single test, it passes just fine. But when I try to run all my 80 tests at once, I get
unable to locate node using //a[starts-with(#href,'/submenuitem'
I guess the submenu is not yet open, htmlunitdriver has too much speed. Somethimes a "You may only interact with elements that are visible is occured on single runs too. Can someone help me fix this issue? Using FirefoxDriver or so is not an option for me.
Using a manual Thread.sleep(time) to wait for selenium actions is a dirty solution and should not be used at all.
Instead you could run a check is the element is visible before interacting with it.
public void waitUntilVisible(WebDriver driver, WebElement element){
WebDriverWait waiting = new WebDriverWait(driver, 10);
waiting.until(ExpectedConditions.visibilityOf(element));
}
public void waitUntilClickable(WebDriver driver, By locator){
WebDriverWait waiting = new WebDriverWait(driver, 10);
waiting.until(ExpectedConditions.elementToBeClickable(locator));
}
Actions builder = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[starts-with(#href,'/index.html')]"));
waitUntilVisible(driver, menu);
builder.moveToElement(menu).build().perform();
WebElement menuItem = driver.findElement(By.xpath("//a[starts-with(#href,'/submenuitem')]"));
waitUntilClickable(driver, By.xpath("//a[starts-with(#href,'/submenuitem')]"));
menuItem.click();
You are using the implicit wait after finding the submenu item. I think there is no use for implicit wait there. The most advisable place to use the implicit wait is to declare after initializing the Driver instance.
One More solution you can use Explicit Wait to wait for an element in the Page.
Refer this post for more info about the Selenium waits.

How to keyPress + click with selenium

I have a problem with my Selenium code not performing correctly the keyPress + click operation.
The test should open the jqueryui.com link and select the first 2 li elements on the page.
I am using Selenium 2.23 and Firefox 10. My code is as follows (I have trie 4 different ways to get it working but none performed):
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver browser = new FirefoxDriver(profile);
browser.get("http://jqueryui.com/demos/selectable/");
List elements = browser.findElements(By.cssSelector("ol#selectable li"));
Actions a = new Actions(browser);
a.keyDown(Keys.CONTROL)
.moveToElement(elements.get(0))
.click()
.moveToElement(elements.get(1))
.click()
.keyUp(Keys.CONTROL)
.build()
.perform();
Keyboard keyboard = ((HasInputDevices) browser).getKeyboard();
keyboard.pressKey(Keys.CONTROL);
List<WebElement> selectOptions = browser.findElements(By.cssSelector("ol#selectable li"));
selectOptions.get(1).click();
selectOptions.get(3).click();
keyboard.releaseKey(Keys.CONTROL);
Actions builder = new Actions(browser);
builder.keyDown(elements.get(0), Keys.CONTROL)
.click(elements.get(0))
.click(elements.get(1))
.keyUp(Keys.CONTROL);
Action selectMultiple = builder.build();
selectMultiple.perform();
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.CTRL_MASK);
elements.get(0).click();
elements.get(1).click();
robot.keyRelease(KeyEvent.CTRL_MASK);
browser.quit();
Can anyone help me with some other suggestions?
This is a bug in Selenium, affecting shift/control/alt combined with clicks on Firefox for Windows. Star the bug and perhaps they will fix it.
I really have no idea why none of your attempts work (particularly the first one). The key constants are a mess.
Anyway, I have been able to make this work (on Windows XP):
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
elements.get(0).click();
elements.get(1).click();
robot.keyRelease(KeyEvent.VK_CONTROL);
I think that`s not a bug.
Try use this (C#):
Action builder = new Actions(driver);
builder.KeyDown(Keys.Control);
builder.Click(element1);
builder.Click(element2);
builder.KeyUp(Keys.Control);
builder.Perform();
or for you(Java):
Actions a = new Actions(browser); a.keyDown(Keys.CONTROL)
.moveToElement(elements.get(0)) .click()
.moveToElement(elements.get(1)) .click() .keyUp(Keys.CONTROL)
.build() .perform();
Just instead of
.Click(); .build(); .perform();
use
a.Click(YourWebElement);
a.keyUp(Keys.CONTROL);
a.build();
a.perform();
Should works,