I have a query on mouseover action in Selenium Webdriver Java.
Consider i have a background image with id "Lfade" with opacity 0.5. If i hover the mouse then a button would be shown.
I want to click on the button to take me to another screen. How do i do this ???
I have tried this, but it does not work
Actions builder = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("Lfade"));
builder.moveToElement(tagElement).build().perform();
Html
div id="homeslant"
div id="wrapper"
div id="lFade" class="learn" style="opacity: 0.5; visibility: visible;"
Button
div class="descbtn"
a class="btn dwmbutton" href="/learn/index.html">KNOW MORE</a>
Not sure about what you are trying to do but I notice an error here:
builder.moveToElement(tagElement).build().perform();
Actually, the .perform() includes a .build().
May be you were referring to .click() so
builder.moveToElement(tagElement).click().perform()
As a pattern for your objective I would:
find the first image
move to the image
wait
find the button
click the button
I used Robot class for mouse hover. Please try the below code.
Robot robot = new Robot();
Point MyPoint = tagElement.getLocation();
robot.mouseMove(MyPoint.getX(), MyPoint.getY());
After that use the normal selenium click to click on the button.
You need to move to the button and perform a click.
Move to the image
wait
Move to the button and click
Actions act = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("Lfade"));
act.moveToElement(tagElement).click().build().perform();
WebElement _button= driver.findElement(By...);
act.moveToElement(_button).click().build().perform();
Try this
#FindBy(xpath="//*[#id='chromemenu']/span/a") WebElement menuHoverLink;
#FindBy(xpath="//*[#id='dropmenu1']/a[1]") WebElement subLink;
Actions maction = new Actions(driver);
maction.moveToElement(menuHoverLink).build().perform();
Thread.sleep(2000);
subLink.click();
Use this code it will work :
Actions builder = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("Lfade"));
WebElement buttonElement = driver.findElement(By.classname("btn"));
builder.moveToElement(tagElement).moveToElement(buttonElement).click().perform();
Try this
public void HoverAndClickObject(WebDriver driver, String property1, String property2, String path) throws SAXException, IOException, ParserConfigurationException
{
//get object properties from the xml file repository
Actions action = new Actions(driver);
String[] element1 = xmlParser(path, property1);
String[] element2 = xmlParser(path, property2);
switch (element1[0].toUpperCase())
{
case "XPATH":
driver.findElement(By.xpath(element1[1])).click();
action.moveToElement(driver.findElement(By.xpath(element2[1]))).click().build().perform();
break;
case "ID":
driver.findElement(By.id(element1[1])).click();
break;
case "NAME":
driver.findElement(By.name(element1[1])).click();
break;
case "LINKTEXT":
driver.findElement(By.linkText(element1[1])).click();
break;
case "CSSSELECTOR":
driver.findElement(By.cssSelector(element1[1])).click();
break;
}
}
Related
this is the example from our application
I am trying this code but not working
WebElement ele=driver.findElement(By.xpath("//a[#title='Menu for Distribution']"));
Actions act=new Actions(driver);
act.moveToElement(ele).perform();
Thread.sleep(3000);
for (int i=3; i<=10; i++)
{
System.out.println(i);
ele1=driver.findElement(By.xpath("//a[#target='frame_main'])[i]"));
ele1.click();
}
Please use the following method to click submenu option,
by calling clickSubMenu("New Consignee");
public void clickSubMenu(String optionName){
By mainMenu = By.xpath("//a[#title='Menu for Distribution']");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(mainMenu));
action.build().perform();
By subMenu = By.xpath("//a[text()='"+optionName+"']");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(subMenu));
driver.findElement(subMenu).click();
}
Steps :
Hover over to the main menu
wait for the visiblity
Click the submenu
I'm having troubles regarding webdriver not being able to click checkboxes sometime and just skipping them, both in Firefox and Chrome.
I've tried different solutions such as
click();
action.moveToElement(checkbox).clickAndHold(checkbox).release().perform();
jse.javascriptExecutor(argument[0].click(),checkbox).
Here I provide the Javascript code I have for the click event
...
var selectCorrectOption = function () {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
$(this).find('> input').prop('checked', false);
} else {
$(this).addClass('selected');
$(this).find('> input').prop('checked', true);
}
};
$('.option > .input-container').on('click', selectCorrectOption);
...
the HTML code where it is attached the javascript click event
<div class="input-container selected" data-choice-id="2">
<input type="radio">
</div>
the Java code data uses a data-attribute to access the element is question. Notice also that once the div is clicked, a 'selected' class appears(which is the current state) on the code below.
WebDriverWait wait = new WebDriverWait(driver, 20);
JavascriptExecutor jse = (JavascriptExecutor)driver;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-choice-id='"+ wrongOptionVal +"']")));
radio=wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("[data-choice-id='"+ wrongOptionVal +"']")));
jse.executeScript("arguments[0].scrollIntoView()", radio);
jse.executeScript("arguments[0].click()", radio);
I expected it to be consistent ,most of the times work, but there are always that one or two times it fails.
I do not know why this happens, I do however know how this can be addressed. You can implement a FluentWait with polling mechanism, which will do three things:
locate the element,
select the radio box,
return getAttribute("class").contains("selected") value.
If getAttribute("class").contains("selected") will result in false the process should repeat.
FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)
.pollingEvery(Duration.ofMillis(300))
.withTimeout(Duration.ofSeconds(10));
fluentWait.until(new Function<WebDriver, Boolean>() {
#Override
public Boolean apply(WebDriver driver) {
WebElement element = driver.findElement(By.cssSelector(radioCssSelector));
element.click();
return element.getAttribute("class").contains("selected");
}
});
I am trying to click the link under the navigation bar. I tried driver.findelement in this code snippet. It selects the link but the click event is not taking place.
WebElement menu=driver.findElement(By.xpath(".//*[#id='bs-example-navbar-collapse-1']"));
//WebElement menu = driver.findElement(By.XPATH("Coplete_navigationbar_xpath")); List<WebElement>
List<WebElement> allLinks = menu.findElements(By.tagName("a"));
String MenuOptn="";
for (WebElement w : allLinks)
{
MenuOptn=w.getText();
if(MenuOptn.equalsIgnoreCase("TRACKING"))
{
// System.out.println("tracking");
w.click();
System.out.println("tracking");
break;
}
System.out.print(w.getText());
}
Try click using javascript
WebElement element = webDriver.findElement(locator);
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", element);
Try below options:
driver.FindElement(By.Xpath("//a[contains(., '<link_text>')]")).click();
or
new Actions(driver).moveToElement(driver.FindElement(By.Xpath("//a[contains(., '<link_text>')]")),10,10).doubleClick().perform();
I hope it will be helpful
I need to click on button on IE and I have used both the cases but none worked for me
driver.findElement(By.xpath("//button[text()='Existing Customer']")).click();
or
driver.findElement(By.xpath("//*[contains(text(), 'Existing Customer')]")).click();
or
WebElement obj = driver.findElement(By.xpath("//button[text()='Existing Customer']")).click();
Actions act = new Actions(driver);
act.moveToElement(obj).build().perform();
HTML Image
In place of "button" tag you can use * symbol, it can represent any tag.
driver.findElement(By.xpath("//*[text()='Existing Customer']")).click();
[or]
WebElement obj = driver.findElement(By.xpath("//button[text()='Existing Customer']"));
Actions act = new Actions(driver);
act.doubleClick(obj).build().perform();
I have react generated web page which has this kind of structure:
div
...
div
div
div class=x_1
<span randomtag=y> text1 </span>
div class=x
<span randomtag=z> text2 </span>
div class=x
<span randomtag=q> text3 </span>
div
div
div
...
div
I am trying to click on the randomtag=z. I have tried:
xpath:
I can find the randomtag y's div with just tossing in the structure up to the div (no other identificators) with .../div[2]/div[1]
I can NOT get the second div with xpath (.../div[2]/div[1] works but .../div[2]/div[2] does not work)
css:
The following is not working either
css=div > span[randomtag="z"]
css=div.x span[randomtag="z"]
I am a bit lost with the possibilities here to select the correct element.
The Robot code is just basic
wait until element is visible | locator (whatever it is)
eg.
wait until element is visible css=div.x span[randomtag="z"]
wait until element is visible xpath=<previous path here>/div[2]/div[2]
Any ideas? Is the react reason for this?
I get "Element not visible in x seconds". For timeout I have tried 5-30 seconds.
EDIT:
I also tried to find the xpath with the search tool (behind dev tools on browser) and it finds the element when I insert either the xpath or css locator to the search.
public static void waitUntilElementIsVisible(WebElement element, WebDriver driver)
{
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
wait.withTimeout(2, TimeUnit.MINUTES);
wait.ignoring(ElementNotVisibleException.class); //make sure that this exception is ignored
Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
System.out.println("Checking for the element!!");
if(element.isDisplayed() != true)
{
System.out.println("Target element is not visible");
}
return element;
}
};
wait.until(function);
}
then call this:
WebDriver el = driver.FindElement(By.css("yourcss"));
waitUntilElementIsVisible(el, driver);