I'm trying to click in some JQUERY elements from a very known website to practice Selenium (http://the-internet.herokuapp.com/jqueryui/menu).
I figured out how to navigate into the menu (not sure if my code is a good solution), however am not being able to click in each last submenu option (PDF, CSV, Excel)
I'm trying something like below:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
String jQuerySelector = "$('a#ui-id-6.ui-corner-all')";
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").get(0);");
//click() also did not work
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").click();");
Your JavaScript function of click is wrong.
Use below javascript syntax
executor.executeScript("arguments[0].click();", WebElement);
Below code works for me:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ui-id-4")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("ui-id-4")));
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
WebElement webElement2= driver.findElement(By.cssSelector("a#ui-id-6.ui-corner-all")); // #ui-id-6 is for pdf, #ui-id-7 csv so on
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", webElement2);
Related
I am trying to click a button in selenium java code and it is not clicking all the time. Apparently this is pretty common issue.
I tried below few solutions:
HTML Code :
<button class="btn btn--action btn--border-white btn--my__calculate" style="display: inline-block;">Final Figure</button>
Solution 1:
WebElement btnWorkout = webDriver.findElement(By.cssSelector(".btn--my__calculate"));
if (btnWorkout.isDisplayed() && btnWorkout.isEnabled()) {
btnWorkout.click();
}
Solution 2 :
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement btnWorkout = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".btn--my__calculate")));
btnWorkout.click();
Solution 3:
WebElement btnWorkout = webDriver.findElement(By.cssSelector(".btn--my__calculate"));
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", btnWorkout);
None of them worked for me.
Other strange thing is above step passes without an error and button doesn't click as expected
Induce WebDriverWait And Following Xapth.
Try following options.
Option1:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
btnWorkout.click();
Option2: Use Action class
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
Actions action=new Actions(webDriver);
action.moveToElement(btnWorkout).click().build().perform();
Option3: Use JavaScript Executor
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", btnWorkout);
Curious that the executeScript() option didn't work for you. Keep using your WebDriverWait from option 2, but instead of the click() method, try using Action Chains. I'm not sure how to write this in Java, but it'll be something like this:
Actions action = new Actions(driver);
action.move_to_element(btnWorkout).click(btnWorkout).perform();
Maybe you have another hidden element with same CSS (class). You can try capture the element by linkText or partialLinkText like this:
WebElement btnWorkout = webDriver.findElement(By.linkText("Final Figure"));
I have tried many codes but none worked for me.The site is built in Angular.
WebElement columnElement = driver.findElement(By.id("i301Indv"));
((JavascriptExecutor)getDriver()).executeScript(
"arguments[0].scrollIntoView(true);", columnElement);
WebElement columnElement = driver.findElement(By.id("i301Indv"));
((JavascriptExecutor)getDriver()).executeScript(
"arguments[0].scrollIntoView();", columnElement);
WebElement columnElement = driver.findElement(By.id("i301Indv"));
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollLeft = arguments[0].offsetWidth", columnElement);
Try this:
Actions actions = new Actions(Webdriver);
actions.moveToElement(webElement).click().build().perform();
This will find the element, move to that element and perform the click operation.
I'm using the following to navigate a menu in Selenium. It works perfectly in Chrome, however in IE, it ends up clicking the menu below my target and the submenu item becomes completely inaccessible.
// Actions not supported by FireFox's Marionette Driver, use chrome or ie.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.id("m7f8f3e49_ns_menu_INVENTOR_MODULE_a_tnode"));
System.out.println("Found the inventory text");
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.id("m7f8f3e49_ns_menu_INVENTOR_MODULE_sub_changeapp_INVENTOR_a"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
And here is where the drive is initialized
System.setProperty("webdriver.ie.driver", "C:\\Selenium\\IEDriverServer64.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
I would like to be able to click on the proper menu item to continue with my testing.
I managed to get a working solution by moving the cursor over from the offset. Not sure how well this will work across screens.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText(arg1));
//<span id="m7f8f3e49_ns_menu_INVENTOR_MODULE_a_tnode">Inventory</span>
System.out.println("Found the inventory text");
actions.moveToElement(menuHoverLink);
actions.moveByOffset(100, 10);
Thread.sleep(1000);
//actions.moveByOffset(45, 0);
WebElement subLink = driver.findElement(By.
id("m7f8f3e49_ns_menu_INVENTOR_MODULE_sub_changeapp_SRVITEM_a_tnode"));
actions.moveToElement(subLink, 100, 12)
.click().perform();
I am running Selenium Web-driver using JAVA and facing an issue with auto-suggest input text field. When I enter a String "books" in the text field, an option would show up. Then I want to click or select the input populated on the auto suggest menu.
Below is the code:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.amazon.com/");
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("books");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("gwcswTooltip")));
List<WebElement> findElements = driver.findElements((By.id("gwcswTooltip").name("books on")));
for (WebElement webElement : findElements)
{
System.out.println(webElement.getText());
}
You need to just pick the right locator.
Add the following line
List<WebElement> findElements = driver.findElements((By.xpath("//div[#id='srch_sggst']/div")));
instead of
List<WebElement> findElements = driver.findElements((By.id("gwcswTooltip").name("books on")));
I want to scroll down my web page and i m using this code to scroll page but it is not working
public ViewBasketSentToMePageObject viewSlideShare() throws InterruptedException {
Thread.sleep(500l);
Actions action1 =new Actions(getDriver());
action1.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0030')).build().perform();
List<WebElement> function = getDriver().findElements(By.xpath("//a [#ng-click='onItemClick()']"));
function.get(13).findElement(By.xpath("//img [#ng-src='resources/images/slideshare-icon-small.png']")).click();
return getFactory().create(ViewBasketSentToMePageObject.class);
}
Looking for help
Try using simple java script below and you can scroll the page.
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");
For scroll down:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 250);");
or, you can do as follows:
jse.executeScript("window.scrollBy(0,250)", "");
Scroll until find the WebElement
Try this:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", your_WebElement);
WebElement element = driver.findElement(By.xpath("//input [#id='giveid']"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
Use this. This will help you to scroll down at the particular element. I had tested on my website even. It is working fine.