Selenium - Java - Element gets click but does not action - java

Very new to automation and have not had issues until now
I have a button that once clicked , a pop up button appears which one could click and it would perform a certain action.
I get to the second button and it seems to click it , however it does not perform the relevant action
My Code
//First Button//
WebElement AddUserSelect =
chromeDriver.findElementBy.id(
"j_idt67:j_idt68:j_idt69:j_idt229:pendingTable:dataTable:0:j_idt280_menuButton"));
AddUserSelect.click();
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Second Button//
WebElement AddUser =
chromeDriver.findElement(By.id(
"j_idt67:j_idt68:j_idt69:j_idt229:pendingTable:dataTable:0:j_idt281"));
AddUser.click();
Element on page when I inspect
<a
id="j_idt67:j_idt68:j_idt69:j_idt229:pendingTable:dataTable:0:j_idt281"
class="ui-menuitem-link ui-corner-all" href="#"
onclick="PrimeFaces.ab({s:"j_idt67:j_idt68:j_idt69:j_idt229:pendingTable:dataTable:0:j_idt281",p:"j_idt67",u:"j_idt67",f:"j_idt67"});return false;"
>
<span class="ui-menuitem-icon ui-icon ui-icon-extlink"></span>
<span class="ui-menuitem-text">
Add
</span>
</a>
Any Assistance would be appreciated..Thank you

As per the HTML you have shared you target the second inner span tag and can use the following Locator Strategy to click on the intended element :
chromeDriver.findElement(By.xpath("//a[#class='ui-menuitem-link ui-corner-all' and starts-with(#id,'j_idt')]//span[#class='ui-menuitem-text']")).click();
Update A
As per your comment update as the previous line of code locates the element, however does not action as an alternative you can use the Javascript Click as follows :
WebElement elem = chromeDriver.findElement(By.xpath("//a[#class='ui-menuitem-link ui-corner-all' and starts-with(#id,'j_idt')]//span[#class='ui-menuitem-text']"));
driver.executeScript("arguments[0].click();", elem);
Update B
Induce a waiter through WebDriverWait as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='ui-menuitem-link ui-corner-all' and starts-with(#id,'j_idt')]//span[#class='ui-menuitem-text']"))).click();

Related

Selenium: Can't scroll to click element that is not visible because of thread error: element is not visible

I am trying to click an element that is not visible and needs to be scrolled down to be visible. To fix this, I have tried to use javascript executor and action, but they do not work because before even scrolling, I get a thread error saying the element is not visible. I have made sure that the xpath to the element is correct and verified the code works with elements that are visible without the need to scroll.
<div class="product-grid-item clearfix" data-alpha="LOG ON T-SHIRT BLACK" data-price="4800" data-i="27">
<a href="/products/8r9ya45zmdwz" class="product-link">
<img src="[//cdn.shopify.com/s/files/1/0923/4190/products/Palace-2019-Autumn-T-Shirt-Log-On-black-1336\_200x200\_crop\_center#2x.jpg?v=1565334138](//cdn.shopify.com/s/files/1/0923/4190/products/Palace-2019-Autumn-T-Shirt-Log-On-black-1336_200x200_crop_center#2x.jpg?v=1565334138)" alt="LOG ON T-SHIRT BLACK" class="img">
</a>
<div class="product-info">
<a href="/products/8r9ya45zmdwz" class="product-link">
<h3 class="title">LOG ON T-SHIRT BLACK</h3>
</a>
<div class="price">
<span class="prod-price">$48</span>
</div>
</div>
</div>
I have tried javascript executor and action
WebElement element = driver.findElement(By.xpath("//*[#data-alpha='" + productName + "' and #class='product-grid-item clearfix']")); //error occurs at this line
int elementPosition = element.getLocation().getY();
String js = String.format("window.scroll(0, %s)", elementPosition);
((JavascriptExecutor)driver).executeScript(js);
element.click();
and
WebElement element = driver.findElement(By.xpath("//*[#data-alpha='" + productName + "' and #class='product-grid-item clearfix']")); //error occurs at this line
Actions builder = new Actions(driver);
builder.moveToElement(element);
builder.click();
builder.build().perform();
Error message:
no such element: Unable to locate element: {"method":"xpath","selector":"//*[#data-alpha='WINDOWLICKER HOOD GREY MARL' and #class='product-grid-item clearfix']"}
Try use WebDriverWait and change the locator with contains, may contain space.
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(#data-alpha,'" + productName.trim() + "') and #class='product-grid-item clearfix']")));
scroll here....
The element seems to be a dynamic element and as you mentioned that the element that is not visible and needs to be scrolled down to be visible so you can use either of the following solutions:
Using WebDriverWait and ExpectedConditions and elementToBeClickable():
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='product-grid-item clearfix' and contains(#data-alpha, 'LOG ON T-SHIRT BLACK')]//a[#class='product-link' and contains(#href, 'products')]/img[contains(#src, 'shopify')]"))).click();
Using WebDriverWait and ExpectedConditions and elementToBeClickable() through Actions:
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='product-grid-item clearfix' and contains(#data-alpha, 'LOG ON T-SHIRT BLACK')]//a[#class='product-link' and contains(#href, 'products')]/img[contains(#src, 'shopify')]")))).click().build().perform();

Element is not clickable at point - other element would receive the click

I have an element on a website which looks like below
<div id="wrapperCategory" class="categoryItemWrapper">
<div class="panel panel-default panel-categoryItem text-center categoryItem disabledItem" ng-class="category.CategoryStyleClass" ng-click="setselectedProduct(productIndex,product,category); setselectedProductAmount(null);" title="Category">
<div class="panel-heading">
Category
</div>
<div class="panel-body">
Price
</div>
<div class="panel-footer availability" ng-class="category.AvailabilityStyleClass">
<span ng-bind-html="category.AvailabilityText">Avail</span>
</div>
</div>
</div>
I need to click on it to get forward. If I manually click on each of these divs or on span website goes forward but SeleniumWebdriver can't click any of them.
I tried click on span and on div with ng-click event buch each time i get an error:
Exception in thread "main" org.openqa.selenium.WebDriverException: Element <div class="panel panel-default panel-categoryItem text-center categoryItem" ng-class="category.CategoryStyleClass" ng-click="setselectedProduct(productIndex,product,category); setselectedProductAmount(null);" title="Category"></div> is not clickable at point (619.2666625976562, 474.23333740234375). Other element would receive the click: <div style="top: 0;left: 0;bottom: 0;right: 0;position: fixed;pointer-events: auto !important;z-index: 10000;" id="cover-1526454140024"></div>
I don't get it.
Can I somehow check which element is clickable and which element overlaps this which I want to click (I dont'see any div with id="cover-1526454140024" in code of the website) ?
Updated:
Unfortunately it still doesn't work after your solutions.
The same exception when trying to click.
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
JavascriptExecutor js = (JavascriptExecutor) Mundial.driver;
js.executeScript("arguments[0].scrollIntoView();", categoryItem);
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(categoryItem));
categoryItem.click();
List<WebElement> selects = driver.findElements(By.tagName("select"));
Select ticketsToSelect = new Select(selects.get(3));
ticketsToSelect.selectByValue("number:2");
It only works in case when I put sleep and scroll down manually. I don't get it.
As per your response :
You will have to scroll down to let the web element available to your script.For that you can use this code :
public static void scrollDown(WebDriver driver, String YoffSet){
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(YoffSet);
}
here you can call this method anywhere from your code.
Then you can use this code to interact with the web element :
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("static ID")));
element.click();
I experienced lot of such issues, as Ankur says, use wait before click
WebElement seems_unclickable = wait.until(ExpectedConditions.elementToBeClickable(By.id(...
One of the way is ExpectedConditions commands
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Yourid")));
More examples can be found at http://toolsqa.com/selenium-webdriver/wait-commands/
Try "scrollIntoView".
public void ScrollElementIntoView(IWebElement element)
{
var js = driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].scrollIntoView()", element);
}
I had the same issue and it was due to a having a non 100% zoom applied to the page body like:
body {
zoom: 90%;
}
Removing the css zoom fixed the issue.

How to open a WebElement from a Hoover Menu Selenium JAVA

Hello I'm new using selenium and I was trying to execute some tests from a web page.
This is my code:
System.setProperty("webdriver.gecko.driver","C:\\DRIVERS\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//Open Portal Fiscal
driver.get("http://150.23.110.111/Retenciones/");
//Find what field and enter the user and password
driver.findElement(By.id("frmLogin:txtUsr")).sendKeys("arrubio");
driver.findElement(By.id("frmLogin:txtPwd")).sendKeys("gnp00gnp");
driver.findElement(By.id("frmLogin:butLogin")).click();
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath(""));
action.moveToElement(we).moveToElement(driver.findElement(By.xpath("")));
I can enter to the page without problem and I can enter the user and the password to login, but there's a hoover menu on the next page that I can´t use and stops my automatic execution.
This is the xpath and the csspath:
xpath: /html/body/div[3]/div/div/form/div/ul/li[1]/ul/li[1]/a/span
csspath: html body div#content div#leftPanel.ui-layout-unit.ui-widget.ui-widget-content.ui-corner-all.ui-layout-west.blankBck div.ui-layout-unit-content.ui-widget-content form#j_id1833690111_27e067e8.blankBck div#j_id1833690111_27e067e8:j_id1833690111_27e0678e.ui-menu.ui-menubar.ui-widget.ui-widget-content.ui-corner-all.ui-helper-clearfix ul.ui-menu-list.ui-helper-reset li.ui-widget.ui-menuitem.ui-corner-all.ui-menu-parent.ui-menuitem-active ul.ui-widget-content.ui-menu-list.ui-corner-all.ui-helper-clearfix.ui-menu-child.ui-shadow li.ui-menuitem.ui-widget.ui-corner-all a.ui-menuitem-link.ui-corner-all span.ui-menuitem-text
And this is the element that appears inspecting the "Búsqueda" button.
<ul class="ui-widget-content ui-menu-list ui-corner-all ui-helper-clearfix ui-menu-child ui-shadow" role="menu" style="display: block; height: auto; z-index: 1013; left: 0px; top: 28px;">
<li class="ui-menuitem ui-widget ui-corner-all" role="menuitem">
<a class="ui-menuitem-link ui-corner-all" href="/Retenciones/main/faces/m_evaPuntual.xhtml" style="width:120px" tabindex="-1">
<span class="ui-menuitem-text">Búsqueda</span>
</a>
</li>
<li class="ui-menuitem ui-widget ui-corner-all" role="menuitem">
</ul>
How can I select and open the button "Búsqueda" from the hoover menu?
Thanks for the attention :)
try using:
Actions action = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("xpath for menu"));
WebElement item = driver.findElement(by.cssSelector("css selector values for Búsqueda"));
action.moveToElement(menu).moveToElement(item ).click().build().perform();
Try this below code using action class
WebElement menu_element = driver.findElement(By.xpath("your_menu_xpath"));
WebDriverWait wait = new WebDriverWait(driver, 10); //Explicit wait method, wait for web-element till 10 seconds so, your driver should able to find the web-element.
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("Your_submemu_xpath"))));
WebElement sub_menu_element = driver.findElement(By.xpath("Your_submemu_xpath"));
Actions action = new Actions(driver);
action.moveToElement(menu_element).moveToElement(sub_menu_element).click().build().perform();
Explanation:
1) First locate the menu element
2) Provide explicit wait method for few seconds so your driver may able to find the sub_menu_element that you want to go with.
3) After explicit wait locate the sub_menu element, that you want to go with.
4) Using Action class try to move element from menu to sub menu.

I was trying to select the next month from the calendar and was unable to do so

This is the code which I was trying to execute.
driver.get("https://easemytrip.com/");
driver.findElement(By.id("ddate")).click();
driver.findElement(By.id("img2")).click();
I am unable to click on the next month of the calendar. Here is the HTML code.
<div class="month">
<div id="dvprevious" class="dvnxt" runat="server">
<img id="img2" onclick="return FillcalendarV(03,2017);" alt="Arrow" src="img/left.png"/>
</div>
<div class="month2">Apr 2017</div>
<div class="month3">
<img id="img1" onclick="return FillcalendarV(05,2017);" alt="Arrow" src="img/right.png"/>
</div>
</div>
My two work arounds were to force a mouse click on the item location.
WebElement elem = driver.findElement(By.id("id"));
Actions action = new Actions(driver);
action.moveToElement(elem).perform();
action.moveToElement(elem).click().perform();
or
driver.findElement(By.id("id")).sendKeys(Keys.ENTER);
The problem is that it takes a second for the calendar popup to get rendered so you need a brief pause. The next problem I ran into is that you apparently can't click the > img because it's blocked by the container DIV. So, I just clicked the container DIV and it worked. The code below works.
driver.get("https://easemytrip.com/");
driver.findElement(By.id("ddate")).click();
new WebDriverWait(driver, 3).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.month3"))).click();
Following code will work.
WebElement ele1 = driver.findElement(By.id("dvfarecal"));
ele1.click();
WebDriverWait wait = new WebDriverWait(driver, 5);
WebElement ele2 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//img[#src='img/right.png']")));
ele2.click();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
WebElement date = driver.findElement(By.id("snd_3_10/05/2017"));
date.click();

WebDriver:Java - How to get all invsible buttons on webpage and click any one button

I am working on a web application which have the div structure as given below
<div class="hover-buttons">
<a id="j_idt59:0:j_idt62" href="javascript:void(0);" class=" btn btn-padding-side" ng-click="click($event,{s:"j_idt59:0:j_idt62",p:"j_idt56"});">
<i class="icon left "></i>
Transfer
</a>
<a id="j_idt59:0:j_idt64" href="javascript:void(0);" class=" btn btn-padding-side gray-dark" ng-click="click($event,{s:"j_idt59:0:j_idt64",p:"j_idt56",u:"breadcrumb mainPage accAddServicesPanel"});">
<i class="icon left "></i>
Account Details
<div scrolltopfunction=""></div>
</a>
</div>
I need to get all the buttons on the page with tag <a id> and click on any one button which have dynamic IDs changing.
The number of buttons displayed on page varies everytime logged In and these are invisible by default. Kindly advice on how to get all buttons and click any one button.
If you want random click from the list of WebElement try using java.util.Random as below :-
import java.util.Random;
Random random = new Random();
List<WebElement> list = driver.findElements(By.id("j_idt59"));
list.get(random.nextInt(list.size())).click();
Edited :- If want to click with certain condition instead of random click try as below :-
List<WebElement> list = driver.findElements(By.xpath("//*[contains(#id,'j_idt59')]"));
for(WebElement el : list){
if(el.getAttribute("id").contains("j_idt64")){
button.click();
break;
}
}
Get all links
Iterate over links & compare for link text you required
Here's the code snippet I think may help you:
List<WebElement> listOfButtons = driver.findElements(By.cssSelector("div.hover-buttons > a"));
for(WebElement button : listOfButtons){
if(button.getText().trim().equals("Your Button text")){
button.click();
}
}

Categories

Resources