Unable to click on webelement button - java

Unable to click on webelement button
I tried to click on button by mouse movement but no success
my outer html is as below :
<button class="btn btn-alt btn-small" type="button" ng-click="ecdapp.uploadBlueprintModalPopup();">
Create
</button>
button xpath is:
//*[#id="page-content"]/div[3]/button

Not seeing the full page source it's hard to tell where your XPath expression is good or not, you can try locating the button using its text instead
//button[normalize-space(text())='Create']
the normalize-space() function is used to discard heading/trailing whitespaces
It might also be the case the button is not immediately available, I would recommend considering using Explicit Wait approach via WebDriverWait class
WebElement myButton = new WebDriverWait(driver, 10)
.until(ExpectedConditions
.elementToBeClickable(By.xpath("//button[normalize-space(text())='Create']")));
myButton.click();
the above code will try to locate the aforementioned button for 10 seconds and click it as soon as it will be present/visible/clickable. Otherwise it will fail with NoSuchElementException

May be the Xpath is wrong. Try the below xpath:
//button[contains(text(),'Create')]

As you can see on the screenshot this Xpath 100% works, if you still won't be able to click on that button, then problem is not with xpath. Let me know if its still fails.
By.xpath("//button[#class = 'btn btn-alt btn-small' and #type = 'button']")
Based on your comment:
I tried this code , but unable to click . element click intercepted: Element ... is not clickable at point (293, 97). Other element would receive the click: ... (Session info: chrome=74.0.3729.169)
I pretty sure I know whats your problem, before u click on this element, something going on the page:
It says - Other element would receive the click, means there is other element above(overlapping) your button(pop up window, page is greyed out(disabled while loading, Some JS running)), so when Selenium trying to click on your button its actually clicking on that blocking element.
Try to click after Thread.Sleep(); wait 5-10 sec.
If this is the case then u need to add condition before find your button to check that element that prevent from clicking on button is disappeared then u click on it.

Try JavaScript executors as below,
WebElement element = driver.findElement(By.xpath("<xpath of button>"));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);

Related

Selenium says element clicked 'successfully', but, it is not actually clicked

I have a scenario where I need to click Continue button.Even I gave print message after click function.The message is printing but the button is not clicking.I have tried java script executor,explicit wait(elementtobeclickable)But still its not clicking.What is the other solution.
This is what I have tried till now
By click_continue= By.xpath("//input[#id='btnWFContinue']");
if(driver.findElement(click_continue)!=null) {
waitVar.until(ExpectedConditions.elementToBeClickable(driver.findElement(click_continue)));
WebElement ele = driver.findElement(click_continue);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
log.info("Clicked on Continue!!!");
/*
WebElement element = driver.findElement(click_continue);
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
driver.findElement(click_continue).click(); */
}else {
log.info("Continue button is not present moving to next step");
}
Open the desired page in your browser and open the browser console . Execute the below code in the same
$("input[id='btnWFContinue']").click() or document.getElementById("btnWFContinue").click()
If the element is clickable , then the above javascript command must change the webpage. Use the same in your script as well. Other there is no click event associated with your element.
try placing a wait statement before your if statement. Its possible that the code executes before the elemnt is loaded and the code jumps to the else part.

Xpath issue in element location

I am getting a very long xpath for an element that I selected. Is there anyway to shorten it? This is the xpath I am getting:
//li[#class='menu_men 1-level hasChild']//div[contains(#class,'level-2')]//div[#class='menu-wrapper']//ul[#class='level-2']//li[#class='1-level']//div[#class='level-3']//ul[#class='level-3']//li//a[#class='level-3'][contains(text(),'Socks')]
This is the URL: Calvin Klein Singapore I hovered over 'MEN', the accessories section will appear, than I hover the 'Socks' to get the xPath.
I am getting the following execption in my code and I am wondering if somehow the long xpath could be one of the reasons:
org.openqa.selenium.NoSuchElementException: no such element: Unable to
locate element: {"method":"xpath","selector":"//li[#class='first
menu_men 1-level
hasChild']//div[contains(#class,'level-2')]//div[#class='menu-wrapper']//ul[#class='level-2']//li[#class='1-level']//div[#class='level-3']//ul[#class='level-3']//li//a[#class='level-3'][contains(text(),'Socks')]"}
I am using cropath from within chrome developer tools to get the xPath.
I am new to automation, I really hope someone can advise. Thank you.
#SameerArora this is the code I have to clear the pop up window, as what I had mentioned in the comments below.
//for clearing the popup window
#FindBy(how=How.XPATH,using="//*[starts-with(#id,'popup-subcription-closes-link-')]")
public WebElement newsletterpopup;
public String clickCategory(){
//.....
resusableFunctions.buttonClick(driver, newsletterpopup, "popoup");
}
public void buttonClick(WebDriver driver, WebElement element, String elementName) throws InterruptedException
{
try
{
element.click();
System.out.println("Log: ResuableFunction.buttonClick");
}
catch (org.openqa.selenium.ElementNotInteractableException notInteract)
{}
The element you are looking for can be found using xpath:
WebElement element = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
However, as the element is not visible directly when you are opening the link, you would be getting NoSuchElementException, so to resolve it you can use javascript click method on the element which directly operates on the div of the page.
Addition to this, i can see that a subscription popup comes when i am opening the page for the first time, so you need to dismiss that popup first(if the popup is present) and then click on the "Socks" element using the JavaScript click method.
Your code should be like:
List<WebElement> closeSubscriptionPopUp = driver.findElements(By.xpath("//a[contains(#id,'popup-subcription-closes-link')]"));
if (closeSubscriptionPopUp.size() > 0) {
closeSubscriptionPopUp.get(0).click();
}
WebElement sockElement = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", sockElement);
To hovered over 'MEN' >> accessories >> 'Socks' section, You need to use selenium Actions class.
As it is not really possible to first click on men(as it will open other section),
So to hover to sock, you need to chain all of the actions that you want to achieve in one go.
Process should be:
move to men element first
Move to accessories
then move to Socks and click on it.
Note: By using Action class, we can chain all the process in one single go.
As mentioned below
1) First way:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]")))
.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")))
.click().build().perform();
2) Second way with wait:
WebDriverWait wait= new WebDriverWait(driver, 10);
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]"))).build().perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.click().build().perform();
Try this:
//a[normalize-space(text()) = 'Socks']
I would recommend you to not use such long xpath's and try to write xpath on your own.
Try :
//li[contains(#class,'menu_men')]//a[contains(text(),'Socks')]

Java Selenium: Get get multiple WebElements of the same class

This is the Button I want to press:
<button class="ProfileTweet-actionButton js-actionButton js-actionRetweet" data-modal="ProfileTweet-retweet" type="button">
The same kind of Button exists multiple time on the site.
I tried pressing it by doing this:
By byaXpath = By.xpath("//div[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]");
WebElement Element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byaXpath));
Element.click();
But after 10 secons I just get this error:
org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for presence of element
Im kinda lost now. I tried diffrent solution to similar cases but nothing seems to work.
You have to put button instead of div at the start of your xpath.
"//button[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]"

Error while clicking button

I am try to click on button but its throwing an error.
Selenium code is
WebElement sa = driver.findElement(By.xpath("html/body/div[2]/div/div[7]/div/div/div[2]/div[2]/a[1]/div/div/div[2]"));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", sa);
Error i am getting is :
Element is not clickable at point (215, 105). Other element would receive the click: <svg style="overflow: hidden; position: relative;" xmlns="http://www.w3.org/2000/svg" width="1287" version="1.1" height="100"></svg>
Please help.
Try to explicit wait the page to load. Or the page is loaded but the element is not visible for some reason. You can to scroll the page to where the element is.
(driver as IJavaScriptExecutor).ExecuteScript(string.Format("window.scrollTo(0, {0});",
elementToClick.Location.Y));
// Click the element
elementToClick.Click();
The example you can find here:
Chrome - Element is not clickable at point #2766
Actually provided exception would be throw when you use .click() of WebElement method instead of using javascript click, So are you sure this exception is throwing in this line when you are going to click using JavascriptExexutor??
Anyway You can try using By.cssSelector() to perform click on this div as below :-
driver.findElement(By.cssSelector("div.question_text")).click();
Edited :- If you're still getting same exception you need to scroll first to reach that element using JavascriptExexutor then click as below :-
WebElement el = driver.findElement(By.cssSelector("div.question_text"));
//Now scroll to reach that element
((JavascriptExexutor)driver).exexuteScript("arguments[0].scrollIntoView()", el);
//now click on this link
el.click();

Unable to click on pop up screen

After entering values to few fields i click on submit button which produces a pop up screen where i should click go button. I tried below code, it worked once but not working now. Please help
WebDriverWait wait = new WebDriverWait(driver, 6);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='lets_go']")));
driver.findElement(By.xpath(".//*[#id='lets_go']")).click();
How to fix ?
If it's an alert the use:
driver.switchto().alert().accept().
If pop up is a window, then first switch to that window using WindowHandler, then click on element
ExpectedConditions.visibilityOfElementLocated is used for checking that an element is present on the DOM of a page and visible, if this condition true within given time limit, it returns WebElement otherwise throws TimeOutException, So There is no need to find element again, omit last line and try as below :
WebDriverWait wait = new WebDriverWait(driver, 6);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lets_go")));
el.click();
I would suggest pasting the snippet of code for that "popup" to get your code working along with stack trace of error you are getting.
Also, you can go through http://www.softwaretestinghelp.com/handle-alerts-popups-selenium-webdriver-selenium-tutorial-16/. It gives you clear idea when it comes to handling popup.

Categories