I am using WebDriver 2 with Java.
I am testing a menu that has hidden sublinks that are visible once a user hovers over them. I need to hover over the menu, click on each link within the menu, go the appropriate page, return back to the original page and repeat all the steps for the next link. I can do this using the following method which does that for each sublink separately. My goal is to create a method that will do all those steps using a loop, that can be re-used for similar menus. Here is the method that I have right now without a loop (I have created custom methods hoverOver and waitForElementVisibility).
hoverOver is:
public void hoverOver(WebElement element){
Actions builder = new Actions(driver);
builder.moveToElement(element).build().perform();
new WebDriverWait(driver,10);
}
waitForElementVisibility is:
public void waitForElementVisibility(WebElement element){
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(element));
}
And the method for menu and sublinks is(this is the method that I want to create as a loop):
public void checkLinksInTopMenu(WebElement menu, WebElement sublink1,
WebElement sublink2, WebElement sublink3){
hoverOver(menu);
waitForElementVisibility(sublink1);
sublink1.click();
driver.navigate().back();
hoverOver(menu);
waitForElementVisibility(sublink2);
sublink2.click();
driver.navigate().back();
hoverOver(menu);
waitForElementVisibility(sublink3);
sublink3.click();
driver.navigate().back();
}
Please help me out, thank you in advance.
if any exceptions, what is the exception?
Actions class will work for mouse over.
no need of wait statement in hoverOver method.
try like the following
mouseover followed by clicking on submenu item.
builder.moverToElement(webElement).click(webElement).build().perform();
if any exceptions, what is the exception?
Actions class will work for mouse over.
no need of wait statement in hoverOver method.
try like the following
mouseover followed by clicking on submenu item.
builder.moverToElement(webElement).click(webElement).build().perform();
if above code works then pass two arguements (webelement1, webelement2) to the method
webelement1 to hover the main menu
webelement2 to click on the submenu item
you can do this by simply calling a method with two parameters.
please let me know if you need any help.
Related
{ actions.clickAndHold(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.DOWN)
.sendKeys(Keys.ENTER).perform(); }
enter image description here
Can't you use something like this? Obviously the selectors I used can be improved. I am not sure why we are clicking and holding and then pressing keys down. When we do this manually, that is not the action that we are taking. We basically move to the Rankings menu, then move to the womens link, then click if needed.
driver.get("https://www.cricbuzz.com");
WebElement rankingMenu = driver.findElement(By.xpath("//a[contains(text(),'Rankings')]"));
WebElement womensSelect = driver.findElement(By.xpath("//a[#title='ICC Rankings Women']"));
Actions action = new Actions(driver);
action.moveToElement(rankingMenu).moveToElement(womensSelect).click().build().perform();
I tested first if the first line of code to click the XPath which works but then when I add a second line of code to click By.name() it doesn't work, so I tried to change in XPath and then in CSS selectors but it only clicks the first one the (XPath code of line). I have tried but it doesn't seem to click the two other elements.
What I found out was that it only clicking what was on the first page, didn't really matter what was on the new page and I told to click on an element that I wanted to do. I'm using the Selenium version 3.141.59.
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ae65255\\Desktop\\java_gui\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://shop.palaceskateboards.com/collections/new");
driver.findElement(By.xpath("//*[#id=\"product-loop\"]/div[#data-alpha='S-LINE JOGGER BLACK']")).click(); //only this one work
driver.findElement(By.name("button")).click(); //second click dosen't work?
driver.findElement(By.linkText("Cart")).click(); //this dosen't work too?
}
Add some wait to let the page load before locating the element
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.name("button")));
button.click();
The third locator By.linkText("Cart") didn't work because the button doesn't have Cart text, it's in the data-text and value attributes.
As a side note, you should use By.partialLinkText() when looking for partial text.
There is a sort option on the website which upon clicking presents options which i want to click on.
Using the click() event for that sort label, does nothing.
That highlighted part is the label which is clickable in image, though click event for the same doesn't work via script.
From that list, i want to click on Most listings option.
Code:
WebElement op= driver.findElement(By.id("sortBy-label"));
op.click();
I am not sure why it is not working without looking into the application. I can come up with something with the shared HTML. Make sure you use explicit waits and wait for the search results to be displayed before clicking on the SORT OPTIONS. Why don't you try this?
//wait for the search results. Use explicit waits and try this
driver.findElement(By.linkText("SORT BY: MOST ACTIVE")).click();
List<WebElement> lis=driver.findElement(By.id("sortBy-menu")).findElements(By.tagName("li"));
for (WebElement li : lis) {
if (li.getText().trim().equals("Most listings")) {
li.click();
}
}
I am looking to click a menu link, but the link only appears if the cursor hovers above it stretching the drop down. Therefore, the automation is unable to click it like it would normally with my click function. I did some research and used moveToElement, and clickAndHold. The latter has given me some hope, but it is far from perfect. I am finding it not clicking at all half the time, and sometimes it does click but clicks a different menu link in the drop down. Any ideas how I can make it work 100% of the time?
public String hoverClick(String object, String data){
APP_LOGS.debug("Moving the mouse");
try{
WebElement tab;
WebElement link;
tab = driver.findElement(By.xpath("//a[contains(#href, 'FOO')]"));
link = driver.findElement(By.xpath("//a[contains(#href, 'BAR')]"));
Actions act = new Actions(driver);
act.clickAndHold(tab).click(link).perform();
return Constants.KEYWORD_PASS;
}catch(Exception e){
return Constants.KEYWORD_FAIL+"Unable to move the mouse/click"+e.getMessage();
}
}
Thank you.
Please give a try with following:
act.moveToElement(tab).moveToElement(link).click(link).perform();
Here, we hover over the element which populates dropdown(and which contains element to be clicked on) and then move to element to be clicked on and then click it.
I have used selenium 2.31.
I have used Actions class for mouse movement. Using this I moved the mouse over a menu and its submenu appeared only for a fraction of second unlike with older version of Firefox .
Because of this issue I cannot select the sub menu using driver.findElement as it throws an exception "element cannot be scrolled into view".
Is there any solution for this?
With the actions object you should first move the menu title, and then move to the popup menu item and click it. Don't forget to call actions.perform() at the end. Here's some sample Java code:
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("Menu heading"));
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.cssSelector("#headerMenu .subLink"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
Another way to go about this is to use Selenium's JavaScript Executor to force the style of the element to be displayed.
An Example of this would be along this lines in C#
//Use the Browser to change the display of the element to be shown
(IJavaScriptExecutor)driver).ExecuteScript("document.getElementById('myId').stlye.display="block");
//navigate to your link that is now viewable
driver.FindElement(By.Xpath('//LinkPath')).Click();
From there, you can find the XPath to your element and use selenium to click on the element. You can cascade this to find children of your main element as well
//(IJavaScriptExecutor)ffbrowser).ExecuteScript("document.getElementById('myId').children[1].children[1].style.display='block'");
Note that this is only possible if you have a hover element that changes the display style when hovered over.
Try this code...
It's c sharp code...
//Webelement is the main menu Link
webElement = driver.FindElement(By.XPath("Your element xpath"));
Actions act = new Actions(driver);
act.MoveToElement(webElement).Perform();//This opens menu list
System.Threading.Thread.Sleep(5000);//This line will help you to hold menu
//This web element is the sub menu which is under main menu
webElement = driver.FindElement(By.XPath("Sub menu path"));
act.MoveToElement(webElement).Perform();//This opens menu list
System.Threading.Thread.Sleep(5000);//Holds menu
//This web element is the option you have to click
webElement = driver.FindElement(By.XPath("Path"));
webElement.Click();
This will be helpful if you are using Ruby.
1.First you need to find element by xpath or id.
2.Then use the method action.move_to().perform.
Here is the code:
hover = WAIT.until{$driver.find_element(:xpath,"xpath")}
driver.action.move_to(hover).perform
This answer helped solve my problem.
My challange was to find a link under a menu option.
The link was not visible until I hovered over the Menu.
This crucial part for me was finding out that in addition to hovering over the menu, I next had to hover on the link in order to interact with it.
List<WebElement> list = driver.findElements(By.xpath("//a"));
for (int i=0;i<list.size();i++){
if(list.get(i).getText().equalsIgnoreCase("cacique intimates M"))
{
new Actions(driver).moveToElement(list.get(i)).click().build().perform();
System.out.println("Clicked on Parent Category");
new Actions(driver).moveToElement(list.get(i)).moveToElement(driver.findElement(By.linkText("SPECIALTY BRAS"))).click().build().perform();
break;
}
}