chain of Actions - unable to select a submenu - java

Maybe somebody can help me figure this out. I want to be able to create a chain of Actions using java and Selenium webdriver. Here is what the source of the webpage looks like:
<li id="menu-item-14" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-5 current_page_item menu-item-has-children menu-item-14">About
<ul class="sub-menu" style="display: none; visibility: hidden;">
<li id="menu-item-43" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-43">Team</li>
</ul>
</li>
Basically when you hover over the About menu a submenu name Team will appear and I want to be able to select the submenu.
This is what my code looks like
WebElement aboutMenu = _driver.findElement(By.id("menu-item-14"));
Actions builder = new Actions(_driver);
Action seriesofactions = builder
.moveToElement(aboutMenu)
.moveToElement(_driver.findElement(By.id("menu-item-43")))
.click()
.build();
seriesofactions.perform();
If I take the second moveToElement the code works just fine. Any ideas will be appreciated?
Update: Java Code used
WebElement menuHoverLink = _driver.findElement(By.id("menu-item-14"));
actions.moveToElement(menuHoverLink).perform();
try {
((JavascriptExecutor) _driver).executeScript("document.getElementByid('menu-item-14')).style.display='block'");
} catch (Exception e) {
e.printStackTrace();
}
_driver.findElement(By.id("menu-item-43")).click();

When you hover over your menu-item-14, the style on the ul class="sub-menu" will change to style="display:block; and possibly remove the visibility from the styled element. That then allows the menu-item-43 to appear for selection.
See this post for more information on using mouse over hovers in selenium.
How to do mouse hover using Selenium WebDriver in Firefox 19?

Related

Problem with button with IE driver and Xpath Selenium with Java

i have the follow problem:
I have a button inside a form (the web page have a login and password text box and a button), this button calls a js funtion and after the login and password validation, calls the main web page. The html code is this (this code is inside of a form calls "login" and method = POST):
<INPUT class="btn btn-mini btn-primary" onclick=submitForm(); type=button value="Sign On">
In Selenium i try with the follow statements, but without success:
driver.findElement(By.xpath("//input[#type='button']")).click();
driver.findElement(By.cssSelector("input[type='button'][#value='Sign On']")).click();
driver.findElement(By.xpath("//input[#value='Sign On']")).click();
when i run the script, login and password text are filled correctly, but the click in the button it's no working.
Could you help me with this?
Thanks!
Gonzalo from Chile
To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.btn.btn-mini.btn-primary[value='Sign On'][onclick^='submitForm']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='btn btn-mini btn-primary' and #value='Sign On'][starts-with(#onclick, 'submitForm')]"))).click();
i found the solution, reading a lots of issues related with this. I'm working with win 10 and IE 11, so, Selenium has a problem with this, because need the size of the text, apps, and others items in 100%. In my case, i had this configuration in 150%. Fixing this, i run the script one again and it works.
The code used is:
WebElement button = null;
List<WebElement> inputs = webDriver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
if (input.getAttribute("value").equals("Log In")) {
button = input;
break;
}
}
if (button == null) {
System.err.println("Cannot find button!");
} else {
System.out.println("Clicking button now!");
button.click();
}
After this, i check another code more efficient:
driver.findElement(By.cssSelector("input[type='button'][value='Sign On']")).click();
This works too.
thanks all off you for your help

How to select an item from the dropdown list through selenium webdriver and java

Update:
I want to be able to click on the options available example in the HTML code. There are two options (id="all_setup_home" and id="developer-console-link"). Currently, the XPath I am utilizing clicks randomly on the dropdown and it takes me to the option 1 page (that's what i want) but it is not very dynamic as my XPath does not target option 1 or 2 but the drop-down. And so if I want to click on the second option I will not be able to.
Anything better than this would be much appreciated.
The workaround that's working for now:
getElementByXPath("Settings").click();
Thread.sleep(3000);
driver.findElement(By.xpath("//ul[contains(#class,'scrollable')]")).click();
Inital work that is still not working:
<!-- begin snippet: js hide: false console: true babel: false -->
HTML
<div class="popupTargetContainer menu--nubbin-top uiPopupTarget uimenuList uiMenuList--right uimenuList--default visible positioned" data-aura-rendered-by="101:185;a" data-aura-class="uiPopupTarget uimenuList uimenuList--right uimenuList--default" aria-labelledby="59:185;a">
::before
<div role="menu" data-aura-rendered-by="95:184;a">
<!--render facet:96:184;a-->
<ul class="scrollable" role="presentation" data-aura-rendered-by="97:184;a">
<!--render facet: 816:0-->
<!--render facet: 882:0-->
<li class="slds-dropdown__item uiMenuItem onesetupSetupMenuItem" role="presentation" id="all_setup_home" data-aura-rendered-by="893:0" data-aura-class="uiMenuItem onesetupSetupMenuItem">....</li>
<!--render facet:826:0-->
<!--render facet:2004:0-->
<li class="slds-dropdown__item uiMenuItem onesetupSetupMenuItem" role="presentation" id="developer-console-link" data-aura-rendered-by="893:0" data-aura-class="uiMenuItem onesetupSetupMenuItem">....</li>
<!--render facet:826:0-->
<!--render facet:2004:0-->
I had a similar issue, I don't use selenium so I cannot write the code for you but after your dropdown.click(); you have to wait a little moment, cause at the moment you do not wait for the list to be open.
As per your comment as you are able to open the dropdown next as per the HTML you have shared to select an item from the dropdown you have to induce WebDriverWait for the items to render within the HTML DOM and you can use the following code block :
String value = "all-setup_home";
WebElement dropdown = driver.findElement(By.xpath("//span[#class='slds-icon_container slds-icon-utility-setup slds-button__icon slds-global-header__icon']"));
dropdown.click();
List<WebElement> options = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[#class='popupTargetContainer menu--nubbin-top uiPopupTarget uimenuList uiMenuList--right uimenuList--default visible positioned']/div[#role='menu']/ul[#class='scrollable']//li[#class='slds-dropdown__item uiMenuItem onesetupSetupMenuItem']")));
for (WebElement option : options)
{
if (option.getAttribute("innerHTML").equals(value))
{
option.click();
break;
}
}
So, I was able to click on the first option getting the XPath from this element:
driver.findElement(By.xpath("//ul[contains(#class,'scrollable')]")).click();
However, this isn't very dynamic as I am unable to click on the second option.

How to click on select options using selenium

I can't click on option element-
website: http://www.oferty.net
When I am trying to select one option in the drop-down list. Unfortunately
I receive the message:
"Cannot click on option element. Executing JavaScript click function
returned an unexpected error"
What did I do wrong with the following code:
Select estateType = new Select(driver.findElement(By.id("ps_type")));
estateType.selectByVisibleText("domy");
Please help.
I quicky check the HTML code of the dropdown you are trying to select and it's not a HTML dropdown. it's custom dropdpown created with CSS and so you can't use the default function provided by Selenium to select Dropdown Value.
<div class="jquery-selectbox-list jquery-custom-selectboxes-replaced-list" style="width: 113px; height: 9em; display: none;">
<span class="jquery-selectbox-item value-998 item-0">rynek pierwotny
</span>
...
</div>
Possible Solutions
Try SendKeys on jquery-custom-selectboxes-replaced-list element and see if it's working, if not
Click on jquery-custom-selectboxes-replaced-list and then scroll to the element you are interested in and click on it.
It's a simulated dropdown list, not native. Selenium Select Class can only work on native dropdown list define by Select Tag. Even there is also embed a native dropdown, but it's hidden, that's why report can't click on option error. And When you operate on the dropdown from page, the embed native dropdown is always hidden, thus actually you not operated on the embed native one.
Try below code:
public void choosePsType(String psType) {
// find the container node of the dropdown list
WebElement psTypeSelect = driver.findElement(By.cssSelector("label[for='ps_type'] + div"));
// click on the selector to expand options
psTypeSelect.findElement(By.cssSelector("span.jquery-selectbox-currentItem")).click();
// choose wanted option
String xpath = String.format(".//span[contains(#class, 'jquery-selectbox-item')][.='%s']", psType);
psTypeSelect.findElement(By.xpath(xpath)).click();
}

InternetExplorer Webdriver with nativeEvents

I have a menu hidden in a button, when the button is clicked, then the menu is shown, the structure of hidden menu is follow:
<button id="buttonID"></button>
<ul class="ulClass">
<li>
<li>
<li>
<li>
</ul>
I want to click on second item, so I did:
webDriver.findElement(By.xpath("//ul[#class='ulClass']/li[2]")).click();
It works fine with FF and Chrome, but doesn't work with IE, the reason because I gave the nativeEvents to false to IE:
capabilities.setCapability("nativeEvents", false);
This capabilities is set for the whole test with IE, without it, the whole test won't work, and now I just need to click on this item but can't because of this capabilities.
Is there any workaround to click on this item but I still skip this capability, because apparently we can't toggle capabilities in run time. Thanks.
I'd try JavaScript instead native clicks
var element=webDriver.findElement(By.xpath("//ul[#class='ulClass']/li[2]"));
Driver.ExecuteJavaScript("arguments[0].click();",element);
Thanks for all suggestions, I solved this with JavascriptExecutor:
JavascriptExecutor js = (JavascriptExecutor)webDriver;
js.executeScript("arguments[0].click();", element);

Selenium / Firefox: Command ".click()" doesn't work with a found element

I tried to find a solution to this thing and I spent a lot of time, but it is almost imposible to me to do that.
The matter: I am using Selenium with Java in Firefox. I need to find an element (a listbox) and click on it. So, the code finds the element, but click action does not work. It works fine in Google Chrome every time, and just sometimes in Firefox (with the same Java code sometimes works, and sometimes does not).
There is the part of code with the element when the program enters on the page:
<div id="size-btn" class="size-btn">
<span class="selected-size">SELECCIONA TALLA </span>
<div class="size-select" style="display: none;">
<table>
<tbody>
<tr id="selecsize_2" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051607">
<tr id="selecsize_3" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051608">
<tr id="selecsize_4" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051609">
<tr id="selecsize_5" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051610">
</tbody>
</table>
<button class="size-guide gaViewEvent gaTrack" data-ga-props="{action:'Seleccionar_Talla', opt_label:'Guia_de_tallas'}" data-href="http://www.anyweb.com/webapp/wcs/stores/servlet/ProductGuideSizeAjaxView?catalogId=24052&categoryId=358056&langId=-5&productId=1047599&storeId=10701">Guía de tallas</button>
</div>
</div>
And there is the part of code that changes when the element is clicked:
<div id="size-btn" class="size-btn opened">
I tried many solutions and sometimes it works, but the next time I run the program, it does not work again.
Some solutions:
It finds the element, but does not run click action. I checked with xpath and cssSelector, and there are unique elements found with those expressions.
driver.findElement(By.xpath("//div[#id='size-btn' and not(contains(#class,'opened'))]/span")).click(); // Also checked with By.cssSelector("span.selected-size")
I though it was because of the time, so I tried to solve it that way.
WebElement we = driver.findElement(By.xpath("//div[#id='size-btn' and not(contains(#class,'opened'))]/span")); // By.cssSelector("span.selected-size")
Thread.sleep(3000);
we.click();
Finally, I was a little bit desperate, and I created a new function to try to do this almost 60 times, looking for the change on the element code and if there was any change, just tried to do click action again.
clickAndWaitWhileElementIsNotPresent(By.xpath("//div[#id='size-btn' and not(contains(#class,'opened'))]/span"),By.xpath("//div[#class='size-btn opened']/span")); // By.cssSelector("span.selected-size")
private void clickAndWaitWhileElementIsNotPresent(By by1, By by2) throws Exception {
for (int second = 0;; second++) {
if (second >= 60)
fail("timeout");
try {
if (isElementPresent(by2))
{
break;
}
else
{
driver.findElement(by1).click();
}
} catch (Exception e) {
}
Thread.sleep(1000);
}
}
There are the images of the element:
Does anybody know how to do that?
Finally I found an answer that works with Firefox as well as Google Chrome.
WebElement we = this.driver.findElement(By.id("size-btn"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", we);
waitForElementPresent(By.xpath("//div[#id='size-btn' and contains(#class,'opened')]/span"));
I am not sure why are you using this Xpath, if you have freedom to change Xpath then record the element using selenium IDE and use Xpath::position from drop down list of target(it picks unique path relative to html header), it will solve problem of dynamic locator. And try below mentioned events.
1- Use clickAt.
2- Use fireevent(focus) and then click. Sometime it happens some element in back ground is getting loaded, when it gets loaded, focus move there hence elementNotVisible error.
3- Use mouseDownRight.
I have the same problem in Firefox. The trick is to click the text inside of not the button itself.
I have some solution, make a class with a robot put there TAB event keys, then call that class. What it does its like a back to focus to the page. For some razon the page lost focus and never find that botton.
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
} catch (AWTException e) {e.printStackTrace();}
You can try to use the Actions class from org.openqa.selenium.interactions:
WebElement element = driver.findElement(By.id("size-btn"));
Actions builder = new Actions(driver);
builder.moveToElement(element).click(element);
builder.perform();
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.click(element);
Action action = actions.build();
action.perform();
This worked for me.

Categories

Resources