Selenium webdriver Assert click element worked - selenium

I am automating a web page, and I am having lots of issues in clicking in a element, so I implemented this
try {
element.click();
} catch (WebDriverException e) {
clickJS(element);
}
clickJS is a method I wrote to click using javascript approach, it usually works, however I am having issues when the expression to click does not throw any exception, but in the future steps will fail because it did nothing. Is there a way for assert a click has 'worked' even though it did not throw any exception.
ps: Iam sure the webelement is clickable
PS: I am using chrome webdriver

There's no generic way to determine if a click was successful because a click could do just about anything... navigate to another page, click a checkbox, dynamically load another part of the page, etc.
In general, I would say that this is not the right approach if you are trying to automate a customer scenario. For example, you attempt a click but didn't anticipate some dialog popping up. Your normal click would throw an exception that another element would receive the click but your JS click would succeed. You shouldn't want that click to succeed because a user couldn't click that element without dealing with the dialog first. This may cause a strange failure down the road that will be hard to trace. Do a "normal" click each time. As you run the script, you will find intermittent failures. Investigate them and find solutions, e.g. wait for some dialog to close because 1 in 10 times it closes slowly and so on. In the end, you will have a more robust suite.

You can check whether your click is actually happened or not in some of the scenarios.
Ex: most of the case click will lead to some change in the UI that might be UI change,HTML source code change or some text might change.if nothing is changing after your click then the click itself is no use.
Solution: take a snap shot before ur click and take a snapshot after click then compare both images if u found any difference then ur click performed successfully else not.

Related

Selenium still clicks on the wrong button even after waiting for visibility and clickability

I have a webpage which I'm trying to fully load with clicking on its "Load More" button. However, even though I'm waiting for both the visibility and the clickability of it, selenium still clicks on the wrong element sometimes, before the Load More button comes to vision, completely opening a new tab as a result. My understanding is that its trying to click on wherever the button would've been if it was scrolled down to it successfully.
The piece of looped code that is trying to get the button and clicks it is down below:
val loadMoreButtonClassifier = By.className(ScraperConstants.LOAD_MORE_BUTTON_HTML_CLASS)
val executor = driver as JavascriptExecutor
while (true) {
loadMoreButton = driver.findElement(loadMoreButtonClassifier)
executor.executeScript("arguments[0].scrollIntoView(true);", loadMoreButton)
wait.until(ExpectedConditions.visibilityOfElementLocated(loadMoreButtonClassifier))
wait.until(ExpectedConditions.elementToBeClickable(loadMoreButtonClassifier))
loadMoreButton.click()
}
(I know it is a while true loop. It is surrounded by a try-catch to end it with not finding the button. It is only there to see if the logic works and I'll change the implementation afterwards to a better one, hopefully a better one.)
Before using executor.executeScript("arguments[0].scrollIntoView(true);", loadMoreButton) I was trying to move to the button via actions.moveToElement(loadMoreButton).perform() which ended up doing the same. I have also tried only checking the visibility and clickability separately, and thought maybe both needed to be asserted before a click is tried, but of course there are no changes between the two methodologies there too. I am out of options except an implicit wait which I do not want to do since it isn't a good practice at all. Is there anything I'm missing here?
EDIT: Solved it with executing javascript to find and click the button

Is the Selenium click() recognized as human or automation?

My scenario which produces the question goes something like below:
I enter a webpage via normal means, next I press on a button, to start a HTML5 application on this webpage, this application is inside an iFrame. On application start I'm being prompted to either turn the sound on or off. At this point there are two possible outcomes:
1. When I answer this prompt manually, new buttons appear in the application window, as expected.
2. When I answer this prompt through automation via Appium, new buttons do not appear.
Now to the question:
To answer the prompt I use the click() method from Selenium. Is it possible that this click() is not considered to be executed by a human and therefore doesn't trigger necessary things? And since I don't have access to the source of the application can I force the Selenium click() to look exactly like a human click?
Here is the code I use to execute the mentioned click:
//Application loading up, hence the sleep
Thread.sleep(5000);
AppiumTestBase.getDriver().switchTo().frame("e_iframe");
Thread.sleep(5000);
WebElement soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
AppiumTestBase.getStandardWaitTime().until(elementToBeClickable(soundOff));
soundOff.click();
The program is able to find and switch in to the iFrame, there are no cross-origin issues either. The AppiumTestBase is just there for initializing the driver, setup capabilities etc. I also tried clicking the element via Actions and JavaScript, but there was no change in behavior.
In C# a workaround I've found to actually take control of the mouse and move it/click with it is to use "Microsoft.VisualStudio.TestTools.UITesting" for the Keyboard/Mouse libraries. From there, you can tell it "Mouse.Click(new Point(X, Y));"and it will move your mouse to that location and click.
Sample Code:
using Microsoft.VisualStudio.TestTools.UITesting;
var soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
Mouse.Click(new Point(soundOff.Bounds.X, soundOff.Bounds.Y));

What to do when the selenium click function is flaky

Has anyone had trouble using the selenium click function? Sometimes it cannot click on an element even though its visible. Sometimes it can click on an element that is underneath another element (that the user cannot click on). What's a good alternative?

Selenium element.click() does not work

Hello: I am using Selenium/Java to grab a PDF from a website. The website does not generate them in advance, but only after I clink on a link. When I do, the web server goes away for a few minutes, and then comes back with the content.
I'm using Firefox, and its built-in PDF viewer. When I click on the download link from the main browser window, it opens another window to receive the PDF content. In a few minutes the child window is filled with the PDF content, and all I need to do is click a download button on the Firefox toolbar, and then press a Save button on the confimration dialog. I have done both of these things, successfully, sometimes.
My problem, sometimes occurs when I execute the code to click on the child window's download button...
WebElement element = driver.findElement(By.id("download"));
element.click();
Sometimes, it just doesn't work. The statement:
driver.findElement(By.id("download"))
...never throws an exception, so it appears to always be successful. Yet, the subsequent element.click() will often not produce the expected results.
I've thrown about 100 darts at this problem, but I can't seem to find one that produces consistent results. I've tried introducing delays, calling findElement several times, trying to use the driver on the child window in ways to confirm its connection to that window (all with positive return values), but nothing seems to help make element.click() on the darn download link successfull.
I have found Selenium to be a rock-solid solution, especially when working through the primary window...it never misses a beat, and I'm really quite impressed about that behavior. This is my first Selenium project, and I hoping someone that has used it a bit more, might have a suggestion for this particular problem.
It's hard to answer this question without additional information about how the child window is populated. If the child window is using javascript to add the button to the page and define its behavior (which is likely), then the element could be actually present on the page when you look for it (i.e. no error will be thrown), but it might not be active yet or prepared to be clicked.
It might be a good idea to look at the element definition in the source for the child page to get a better idea of how the button is coded. If you have access to the web developers who designed/implemented the button even better. If you can wait to perform the click action until the button is in the desired state, this should solve the problem.
Additionally if the page is coded using a dynamic framework like Angular, you might be better off using Protractor for testing, which is based off of Selenium, but which is aware of updates in the view as they are occurring.

Selenium stops running after click() function runs

So I am writing a class in eclipse using selenium and autoitx4java libraries, now I have been having an issue that I believe is selenium specific (meaning having nothing to do with autoIt).
Basically when I arrive at the splash page for the website I am attempting to test, the first thing I do is find the element for the login page via XPath, and click on it using the click() method. This is supposed to open up a certificate window.
Once the click() method runs a certificate window comes up, however, for some reason the selenium test does not keep running lines of code. Instead it pauses at the line the click function was executed. The code looks a bit like this:
WebElement login = driver.findElement(By.xpath("example_xpath"));
login.click();
// login.sendKeys(Keys.ENTER);
System.out.println("login clicked");
Class.selectCert();
(Where "Class" is just a class created for this specific test.)
So when the code is run in Selenium, the line "login clicked" is never printed out. As you can see, the alternate sendKeys(Keys.ENTER) function has been tried. When I used this function, literally nothing happened, and the code continued as if that line of code did not exist.
Note: When I highlight the element and manually press ENTER, the certificate window actually pops up.
This is where things get weird. When I remove the line for the click function, and replace it with a pause() function which gives me ample time to click on the element manually. The code runs perfectly fine, the "login clicked" is printed out, and the certificate window is handled by the autoIt code I have in the selectCert() function.
I have tried clicking on different elements on the screen, and so far this bug only occurs when clicking on elements that have an xpath with /img at the end of it. Not sure if that helps at all... Any help would be appreciated!