Selenium webElement equals: does it compare the state of the element? - selenium

how does equals work on webElement, can I use it to check if the same element has been completely loaded?
For elements with animation, would equals return true imply the element returned by findElement at different time stamp are identical in look?
Say
webElemenet ele1 = driver.findElement(By.class("loading"));
sleep(10);
webElemenet ele2 = driver.findElement(By.class("loading"));
Would ele1.equals(ele2) == true, imply the element is completely loaded?

The equal method indicates if two web elements are referring to the same instance of an HTML element in the page.
Whether the content of the HTML elements are different or not has no impact.
So no you can't use it to check the state of an element.
To check the state of an element, you'll have to read the attributes/properties or the text content.

The fact that the element returned is not null indicates that the element exists in the DOM (document object model). Loading of the element would be complete, however there can sometimes be another hidden web element deliberately blocking access to the element until the page has completely loaded or ajax controls are finished processing. For that, you'd need to check its clickable status through a fluent wait command.

Related

Dont get all the Web elements in the Arraylist of webelement selenium webdriver [duplicate]

Selenium: How selenium identifies elements visible or not? Is is possible that it is loaded in DOM but not rendered on UI?
I would like to verify a scenario where element is clickable, I know web drive has method "ElementToBeClickable" however, I would like to know the inner working. Please help me on this.
Also,how to handle a scenario where the element is loaded in the DOM but UI shows loading in progress, how to wait for complete load?
Please let me know, if the UI is not loaded then will selenium directly call the DOM element or if UI element is being loaded then it will fail the execution? I would really appreciate more technical explanation on this.
Selenium can identify the presence or visibility of the elements as soon as they are present or visible in the HTML DOM. From user perspective you can invoke isDisplayed() method on an WebElement to examine if the intended WebElement is displayed or not. As per current implementation Selenium may not be distinguishing between loaded and rendered elements. The ElementToBeClickable method in ExpectedConditions class sets an expectation for checking if an element is visible and enabled so that you can click it.
When the element is loaded in the DOM but UI shows loading in progress you still have to wait for the JavaScript and AJAX Calls to complete loading the page so all the WebElements on the page becomes interactable. At most to wait for complete load you can set the pageLoadStrategy to normal but may still have to induce WebDriverWait for the intended WebElement to become present, visible, interactable or clickable.
Here you can find a detailed discussion on Page load strategy
Of-coarse if the UI is not loaded Selenium may not be able to interact with a few of the DOM elements.
Update
As per your counter question here are the different stages of an WebElement and the respective ExpectedConditions to check the stages :
Presence of an element :
presenceOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
Visibility of an element :
visibilityOf(WebElement element)
An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
Element to be Clickable :
elementToBeClickable(By locator)
An expectation for checking an element is visible and enabled such that you can click it.
Note : As per the docs Element is Clickable - it is Displayed and Enabled.

Robot Framework- Loading Spinner selector

I have this problem where I have to validate if a loading spinner is present, it's present for about 1 second on the page, i have found the xpath selector of the loading spinner but selenium library could not find it is there another way to find out a selector of something that dissapears after a short while? Note: The xpath is definitely correct. There is no id on the loading spinner either.
This is the code i have tried
Validate Loading Spinner
Wait until page contains xpath=//*[#id="app"]/div/div[1]/div[3]/div/div/div/div/svg
I have also tried Element should contain and Page should contain but that does not find the locator.
You should be using one of the keywords that is validating an element is present - Wait Until Element Is Visible, or Wait Until Page Contains Element - both of which support a timeout argument, for how much to wait.
Afterwards, you'd better use the opposite keyword - Wait Until Element Is Not Visible, to make sure the spinner disappears and you can continue with the test.
There is a problem with your locator - xpath has some issues if the element is svg, most of the times it can't address it directly. So instead of specifying it explicitly in the path, look for a node whose name happens to be "svg"; e.g.:
xpath=//*[#id="app"]/div/div[1]/div[3]/div/div/div/div/*[local-name() = "svg"]
(^ changed the last element in the path)
Slightly offtopic - try to have less rigid locators - this one specifies an absolute path from the element with id "app" and down (a div child, then its first div child, then that one's third div child, and so on and so forth). If the element structure changes even slightly, the locator will stop working (say, in a bug fix, or re-positioning it, or just with using a HF of an JS library).
Try to find an element that's 1-2 levels higher than your target svg - by a solid class value, or structure that's unique, and use it as an anchor.
I reckon you used wrong keyword
Validate Loading Spinner
Wait until page contains ELEMENT xpath=//*[#id="app"]/div/div[1]/div[3]/div/div/div/div/svg
Both work:
Validate Loading Spinner
wait until page contains element xpath=//*
[#id="app"]/div/div[1]/div[3]/div/div/div/div/*[local-name() = "svg"]
and
Wait until page contains ELEMENT xpath=//*[#id="app"]/div/div[1]/div[3]/div/div/div/div/svg
I had a very similar issue

What is the Difference Between Wait until Visible and Wait until Located in Selenium

I am using both wait(until.elementLocated(element, timeout)) and wait(until.elementVisible(element, timeout)). The 'wait until visible' one is failing in places where 'wait until located' does not. Why?
As your question is the difference between wait(until.elementLocated(element, timeout)) and wait(until.elementVisible(element, timeout)) and you haven't tagged any Selenium binding, I will explain it from a Java perspective.
until.elementLocated() is equivalent to presenceOfElementLocated() in Java. It checks that an element is present within the HTML DOM of a page. This does not necessarily mean that the element is visible. So there's no guarantee that it's interactable.
until.elementVisible() is equivalent to visibilityOfElementLocated() in Java. It checks that an element is present within the HTML DOM of a page and is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. Again this does not necessarily mean that the element is interactable, i.e. clickable.
For more details on ExpectedConditions in Java, see the docs.
Wait until visible does exactly that. It waits until the element is visible. An element can be in the DOM but be hidden. In that case, it would be able to be located but not visible.
Both until.elementLocated(element, timeout) and until.elementVisible(element, timeout) are used to get the element.
But I would guess that elementLocated will be faster because it's just check that an element is present on the DOM of a page and not necessarily mean that the element is visible. while the elementVisible has to check that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width.
Hope this will explain the difference.

Selenium: How selenium identifies elements visible or not? Is is possible that it is loaded in DOM but not rendered on UI?

Selenium: How selenium identifies elements visible or not? Is is possible that it is loaded in DOM but not rendered on UI?
I would like to verify a scenario where element is clickable, I know web drive has method "ElementToBeClickable" however, I would like to know the inner working. Please help me on this.
Also,how to handle a scenario where the element is loaded in the DOM but UI shows loading in progress, how to wait for complete load?
Please let me know, if the UI is not loaded then will selenium directly call the DOM element or if UI element is being loaded then it will fail the execution? I would really appreciate more technical explanation on this.
Selenium can identify the presence or visibility of the elements as soon as they are present or visible in the HTML DOM. From user perspective you can invoke isDisplayed() method on an WebElement to examine if the intended WebElement is displayed or not. As per current implementation Selenium may not be distinguishing between loaded and rendered elements. The ElementToBeClickable method in ExpectedConditions class sets an expectation for checking if an element is visible and enabled so that you can click it.
When the element is loaded in the DOM but UI shows loading in progress you still have to wait for the JavaScript and AJAX Calls to complete loading the page so all the WebElements on the page becomes interactable. At most to wait for complete load you can set the pageLoadStrategy to normal but may still have to induce WebDriverWait for the intended WebElement to become present, visible, interactable or clickable.
Here you can find a detailed discussion on Page load strategy
Of-coarse if the UI is not loaded Selenium may not be able to interact with a few of the DOM elements.
Update
As per your counter question here are the different stages of an WebElement and the respective ExpectedConditions to check the stages :
Presence of an element :
presenceOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
Visibility of an element :
visibilityOf(WebElement element)
An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
Element to be Clickable :
elementToBeClickable(By locator)
An expectation for checking an element is visible and enabled such that you can click it.
Note : As per the docs Element is Clickable - it is Displayed and Enabled.

What ExpectedConditions should i use in order to

So sometimes when i want to click on WebElement i am using elementToBeClickable.
Now when i want to get text etc. i have 2 options (maybe more ???) that i usually use:
presenceOfElementLocated - An expectation for checking that an element is present on the DOM of a page.
visibilityOfElementLocated - An expectation for checking that an element is present on the DOM of a page and visible.
My questions:
Whats the different between the both ?
When i want to get text from element/attribute maybe should i use another ExpectedCondition ?
presenceOfElementLocated would just wait for the presence of an element in the DOM tree.
visibilityOfElementLocated would not only ensure that an element is present, but also check if the element is displayed. The logic behind the visibility determination is described here:
Element Displayedness
Which Expected Condition to use is not that simple as in case of elementToBeClickable and a button needed to be clicked - in this case depends on the actual use case - how the desired element is loaded, is it loaded with the text, or the text is set later and dynamically etc.
There is also textToBePresentInElement which might be more suitable, but it requires you to know a part of the element's text.
And, there is always an option to write a custom Expected Condition - for instance, you can wait for any text to be present in element.