How to enable button do verify in Selenium WebDriver? - java

I have one button section product, but it disables when hover mouse on section product then the button is enabled. I don't know write script do enable a button when Selenium WebDriver finds it.
#Test(priority = 4)
public void TestSelectedItem() {
driver.findElement(By.xpath("/html/body/app-root/ng-component/div/product-list/div/div/div/div/div[2]/div/div[3]/div/div[1]/a")).click();
Select drpItem = new Select(driver.findElement(By.id("pa_colors")));
drpItem.selectByIndex(2);
driver.findElement(By.className("add_to_cart_button")).click();
}
Please help me.

I will give you code in Python which do hover action on the element:
hover_company = driver.find_element_by_xpath("your XPath")
driver.execute_script("arguments[0].scrollIntoView(true);", hover_company)
hover = ActionChains(driver).move_to_element(hover_company)
hover.perform()
First two lines scroll to your element, last two lines -- do hover action on the element.
After this action -- you can click on the button. Try on.
Code in Java:
hover_company = driver.findElement(By.xpath("your XPath"));
driver.executeScript("arguments[0].scrollIntoView(true);", hover_company);
Actions hover = new Actions(driver);
hover.moveToElement(hover_company).perform();

Related

How to make a click of a few seconds

First post ...
I discovered javascript and selenium, I'm trying to make a left click of a duration of 1 or 2 seconds.
It is easy to make a right click or a double click, but how to make a long click?
Thank you for your support.
Double click is ok :
Actions action = new Actions(driver);
WebElement link = driver.findElement(By.ID ("Element ID"));
action. doubleClick (link).perform();
Click with executeScript ok:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector(script).click();",Arguments);
At this point I have no track for the long click ...
You can try this below option
public void loingClick(WebDriver driver,WebElement element, int numberOfSeconds) throws Exception
{
Actions action = new Actions(driver);
action.clickAndHold(element).build().perform();
Thread.sleep(1000*numberOfSeconds);
action.moveToElement(element).release();
}

hover over an animation menu and click a menu item in selenium

I am trying to hover over an Animation Menu and select an Item in the Menu. I tried to perform hover on the menu by xpath first and perform click on the menu item by xpath as below.
WebElement ch = driver.findElement(By.xpath(".//*[#id='menu-item-24463']/a"));
builder.moveToElement(ch).perform();
WebElement ch1 = driver.findElement(By.xpath(".//*[#id='menu-item-24463']/div/ul/li[1]/a"));
ch1.click();
I get an exception
org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Offset within element cannot be scrolled into view
I also tried to chain the actions as below
builder.moveToElement(ch).moveToElement(driver.findElement(By.xpath(".//*[#id='menu-item-24463']/div/ul/li[1]/a"))).click().build().perform();
which also throws the same exception.
Any Idea to achieve the click in Animation Menu item?
Locate and Store Web Elements
WebElement ch = driver.findElement(By.xpath(".//*[#id='menu-item-24463']/a"));
WebElement ch1 = driver.findElement(By.xpath(".//*[#id='menu-item-24463']/div/ul/li[1]/a"));
Actions builder = new Actions(driver);
Perform hover
builder.moveToElement(ch).perform();
Wait for the Element to appear in the view & Perform Click
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(ch1));
ch1.click();

How To Perform Multiple Radio Button Click in Selenium Webdriver

How to make condition to perform multiple radio button click (see image below)...?
multiple radio button click
I've tried for hours, but I got only single radio button click (see attached image)
single radio button click
List<WebElement> radiobutton = driver.findElements(By.xpath("//*[#type='radio']"));
System.out.println("Total element is " + radiobutton.size());
for (int i = 0; i < radiobutton.size(); i++) {
// if you are getting stale element exception because of the page
// reload
radiobutton = driver.findElements(By.xpath("//*[#type='radio']"));
System.out.println(radiobutton.get(i).getAttribute("value"));
// select your radio and click to go to next page
radiobutton.get(i).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//input[#id='btnCheckFare']"))
.click();
Thread.sleep(3000);
}
I saw your video.You have multiple groups of radio button and you can select one radio button in each group.
You have to use 2 loops,one for group and one inner loop for select radio button in a group.
If you have experience in programming then you can do it easily.Otherwise provide the link where we can see this functionality.

Unable to click a mouse hover link using Selenium WebDriver with Java

I am trying to click a mouse hover link using the code below. The webdriver (v.2.35) doesn't throw any error but the element isn't clicked. Can somebody help me figure out what's wrong?
String URL = "http://www.kgisliim.ac.in/"
String menu ="Alumni>Register"
driver.get(URL);
String[] menuItems = menu.split(">");
Actions actions = new Actions(driver);
WebElement tempElem;
for (int i =0 ; i< menuItems.length ; i++) {
tempElem = driver.findElement(By.linkText(menuItems[i].trim()));
actions.moveToElement(tempElem).build().perform();
}
actions.click();
actions.perform();
NOTE: The above code works fine in the below scenario
String URL = "http://www.flipkart.com/"
String menu ="Clothing>Jeans"
You can try this:
WebDriver driver=new FirefoxDriver();
driver.get("http://www.kgisliim.ac.in/");
Actions actions=new Actions(driver);
WebElement menuHoverLink=driver.findElement(By.linkText("Alumni"));
actions.moveToElement(menuHoverLink);
//driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
WebElement subLink=driver.findElement(By.cssSelector(".options>ul>li>a"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
Since the menu on http://www.kgisliim.ac.in/ takes a second to slide out, you could add a WebDriverWait to make sure the submenu has time to become visible before moving the cursor to it. Try replacing the first line in your for loop with the following line. This will wait a maximum of 5 seconds for the submenu (but will return the WebElement as fast as possible within that time).
tempElem = new WebDriverWait(driver, 5).until(ExpectedConditions
.elementToBeClickable(By.linkText(menuItems[i].trim())));
I stumbled across a similar issue recently, with phantomJS and ghostdriver. In my case, the problem was the window size - the HTML element was outside the visible area and my mouse movements were having no effect (default size is 400x300, which is rather small).
You can check the window size with
driver.manage().window().getSize()
And you can change it with
driver.manage().window().setSize(new Dimension(width, height));

How can I perform mouse click on Mouse Hover of Dynamic Menu items in Selenium 2.0

I am working on Walmart automation using Selenium WebDriver. I have written a function to hover over the Departments Menu "Home , Furniture & Patio" so that it gets highlighted and I can click on "Appliances" link. Here is the function that I have written but it does not seem to hover over the element.
public void NavigateDepartments(){
WebElement ApplianceLink = driver.findElement(By.xpath("//*[div='Home, Furniture & Patio']"));
Actions myMouse = new Actions(driver);
myMouse.moveToElement(ApplianceLink).build().perform();
ApplianceLink.click();
}
I also tried giving absolute path for
Xpath("/html/body/div/div/div[3]/div/div/div/ul/li[3]/div/div") to find the element and it did not work either. Am I missing anything ?
You should first hover on the main menu, then move to the new element
WebElement menu = driver.findElement(By.xpath("//path to *appliance*"));
WebElement parentMenu = driver.findElement(By.xpath("//*[div='Home, Furniture & Patio']"));
Actions builder = new Actions(driver);
builder.moveToElement(parentMenu).moveToElement(menu).click().build().perform();

Categories