What to do when the selenium click function is flaky - selenium

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?

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

Inspect Element shows elements for just a few seconds before all of the rows collapse

It was suggested that this had already been answered and then my question was closed.
I've looked at all of the links and I don't see an answer there. Please don't close my question unless you're absolutely sure it's been answered.
Why XPath does not highlighted the yellow mark in Chrome84? (2
answers)
Chrome 84 Inspect element, find results not highlighted in
yellow like before (6 answers) Chrome DevTools not find elements not
search)
I'm writing some Selenium code and need to get the CSS Selector for an item on a page. So I bring up the page and go into Tools mode. It doesn't matter which browser I use - Chrome, FireFox, Edge - when click on the "Pick an element from the page" button (to the left of the Inspector in FireFox) and then click on the item I need the CSS Selector for I see the element in the tools section, but as I'm right-clicking it and choosing Copy->CSS Selector, the table it's in collapses (in a second or two) and I no longer have access to the item I clicked on. I've tried manually expanding the TR in the tools view and drilling down to the TD I need, but the same thing happens.
Anyone know of a way to stop this from happening? I was quick enough to copy a few yesterday but for some reason it seems to be happening faster today and I'm not able to catch it.
Right-clicking the item and choosing Inspect Element was also suggested but that does the same thing. The item shows up in the tools section for about 2 seconds and then the table collapses.
When the element tree collapses, it is a sign that it got updated. This happens, for example, when an iframe's URL or document is changed or the child tree structure gets replaced via JavaScript.
In order to stop this from happening, you can stop the JavaScript execution on the next execution once you see the element you want to inspect. Then you are able to inspect the element using the inspect button.
Firefox
Switch to the Debugger panel.
Once you see the element you want to inspect, click the Pause button or press F8.
Cause the JavaScript execution to stop (e.g. by interacting with the page). You can see that the JavaScript execution is stopped when there is a white overlay on the page and a hint saying Paused at Execution.
Once the execution is stopped, click the Pick an element from the page button in the toolbar or press Ctrl+Shift+C.
Click the element you want to inspect.
Chrome/Edge
Switch to the Sources panel.
Once you see the element you want to inspect, click the Pause script execution button or press F8.
Cause the JavaScript execution to stop (e.g. by interacting with the page). You can see that the JavaScript execution is stopped when there is a hint on the page saying Paused in debugger.
Once the execution is stopped, click the Select an element in the page to inspect it button in the toolbar or press Ctrl+Shift+C.
Click the element you want to inspect.
I found a semi-workable solution. When the item appears in the tools window note it's name before it disappears. Then go to the Search HTML box and type it's name. It still appears and then disappears, but it seems to stay visible longer.

Can selenium click somewhere which is out of view

I have a code where it is identifying the button but not able to click on it ,may be because it is not in view.If we scroll it clicks.Can selenium click which is out of view
If element is on the page but not on the screen, selenium should click it without problems. Maybe your button is not on page at time of click?
I think it depends on the library you are using. E.g. nightwatch tries to scroll the element into view before clicking
.click()
Suggest edits
Simulates a click event on the given DOM element. The element is scrolled into view if it is not already pointer-interactable. See the WebDriver specification for element interactability.
https://nightwatchjs.org/api/commands/#click
I believe Mink2Selenium does not.
but if the element is not reachable by scrolling, selenium will not be able to click it, same as a user would not be able to click it. What is good, because selenium is used to do as much as possible realistic tests

Selenium webdriver Assert click element worked

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.

selenium IDE displays hidden div then immediately closes it

I have a hidden div element that when I click the link the visibility changes. In my Selenium IDE script I click the link and I see the div open but then it immediately closes. The rest of the scripts run, but in a demo I would like to keep that div open.
My steps are:
click [Link that shows hidden div]
-->It is here that it displays and then immediately hides it.
focus [element inside div]
assertValue [target element]
Is there a setting that I need or a step that needs to be added?
It's hard to suggest. But there are some things that you can try.
First of all maybe your div is going to hide when link is not under
the mouse. Than you can use mouseOver
Maybe if you need just to verify variable you do not need even see
the div. Selenium IDE can take the value from element that is not
visible. It is not a fare play but sometimes you can do it.
Another way is to use javascript to bring your div to the light:
getEval | window.document.getElementById('your_divs_id').set_attribute('style','');
The most brutal and not trustable way to try to do it to add command
getEval | window.stop()
right after the click.