How to wait until the the page loads in Selenium IDE other than waitforpageload? - selenium

I have a problem when running the test cases because the time take for loading the page is different in each time. I have tried WaitforPageLoad and also checking for visual elements but it was not helping.
WaitforPageLoad always wait for specific seconds and checking visual elements wont work all the time since after loading the page there won't be any new elements in some pages.
Could anyone suggest a better idea ?

You could increase the amount of time you wait for a page load:
setTimeout(timeout)
Arguments:
timeout - a timeout in milliseconds, after which the action will return with an error
Specifies the amount of time that Selenium will wait for actions to complete.
Actions that require waiting include "open" and the "waitFor*" actions.
The default timeout is 30 seconds.
This might help. It's a bit unclear what your goal is.

Use the Explicit Wait on your action. For instance:
// Tell webdriver to wait
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(4));
// Test the autocomplete response - Explicit Wait
IWebElement autocomplete = wait.Until(x => x.FindElement(By.ClassName("ac-row-110457")));

Related

How to give Fixed wait in playwright With out any condition Like we had in cypress : Cy.wait(600)

How to give Fixed(Implicit Wait) wait in playwright
With out any condition
Like we had in cypress :
Cy.wait(600);
Thank You
Like we had in cypress :
Cy.wait(600);
To give a fixed wait in Playwright without using any conditions, you can use the page.waitForTimeout method. This method will pause the script for a specified amount of time before continuing.
await page.waitForTimeout(2000); // waits for 2 seconds
You can also use the page.waitForFunction method with a function that returns true after a certain amount of time. For example:
await page.waitForFunction(() => {
const now = Date.now();
return now - start > 2000;
});
This will wait for 2 seconds before returning true.
Note that these methods should be used sparingly as they can cause delays in the script execution. It is generally better to use specific conditions to determine when to continue, such as waiting for a specific element to be present or for a page to load.
This is 5 sec timeout
await new Promise(resolve => setTimeout(resolve, 5000));
Avoid using hard waits with playwright!
Issue with Hard waits:
Hard waits do only one thing , they simply wait for specified time without considering any application actual response time which is very rigid approach.
See the below example:
await page.waitFor(5000); // hard wait for 5000ms
This will wait for 5 seconds regardless of actual response time which may be less or more than 5 seconds which is bad in both cases as in first case it has to wait unnecessary for longer time where object is already loaded and in second scenario it will fail after waiting unnecessary for 5 seconds if object is not going to load at all in failing scenario.
Recommended Approach
In-built waits
Playwright has in-built waiting mechanism on navigation and page interactions. As they are part of playwright itself , it is better to use them and customize it if required.
Explicit waits
There is another way to handle waits explicitly only in scenarios where auto-waiting is not a sufficient or efficient approach to handle depending on specific scenarios but in general it should be avoided if auto-waiting is enough for the requirement.
Waiting for page navigations and API responses
1)page.waitForNavigation to wait until a page navigation (new URL or page reload) has completed.
2)page.waitForLoadState This waits until the required load state has been achieved.
3)page.waitForURL This waits until a navigation to the target URL.
4)page.waitForResponse waits till the response from the given API is received.
Wait for element
In lazy-loaded pages, it can be useful to wait until an element is visible with locator.waitFor(). Alternatively, page interactions like page.click() auto-wait for elements.
// Navigate and wait for element
await page.goto('https://example.com');
await page.getByText('Example Domain').waitFor();
// Navigate and click element
// Click will auto-wait for the element
await page.goto('https://example.com');
await page.getByText('Example Domain').click();
Waiting on page events
We can also directly wait on page events using page.waitForEvent.
Customized Wait Function
At last we can also write (if really needed) customized wait functions using WaitForFunction for very specific scenarios or specific applications or controls.

Wait untill Page_loaded in selenium

I am new to selenium and building a project (python + selenium). I have inserted a custom wait in code like time.sleep(10)
The thing i am trying to impliment is the code should be block untill a page has been loaded fully and same after button clicks.
I have gone through few reading like
implicit wait
explicit wait
wait untill an element appears up (select by id or something else )
Is there any way to block the code untill a page has been loaded fully. (I do not have any condition upon which i can wait for wait untill). I can not use stuffs implicit wait or explicit as there is no fix time for completion of loading a page
def run(self):
self.browser.get('url')
# here it should wait untill the page has been loaded fully
time.sleep(10)
element = self.browser.find_element_by_css_selector('some-css-selector')
https://www.selenium.dev/documentation/en/webdriver/page_loading_strategy/
normal This will make Selenium WebDriver to wait for the entire page
is loaded. When set to normal, Selenium WebDriver waits until the load
event fire is returned.
By default normal is set to browser if none is provided.
so this is done automatically as page load strategy is normal by defaul ,
But it won't consider asynchronous elements taht loads after the page get loaded , if you want to explicitly wait for some asynchronous element use explicit wait as:
WebDriverWait(driver,15).until(EC.presence_of_all_elements_located(By.CSS_SELECTOR,"some-css-selector"))

Implicit Wait Timeout not working

Was trying to block execution for 10 seconds properly with VB.Net Selenium, so found out about Implicit Wait on SO, and found this example.
driver.Manage.Timeouts.ImplicitWait = TimeSpan.FromSeconds(10)
Debug.WriteLine(driver.PageSource)
The problem is, I set a break point on both lines and Debug.WriteLine is called nearly instantly. I've read on here I shouldn't use Thread.Sleep here, so why is the timeout not having the desired effect?
Thanks!
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
May be you missed the parentheses:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
You can also try using below:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
Alternatively you can use ExplicitWait:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(--ELEMENT TO BE VISIBLE--));
It is more extendible in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the prebuilt ExpectedConditions to wait for elements to become clickable, visible, invisible etc.
Solution is in C#.NET. You might need to convert some syntax in VB.NET.

Browser runs faster than webdriver selenium command

I am performing an operation using selenium webdriver to wait for an element until an element is visible. After a few milliseconds, it gets disappeared(Expected).Generally we use explicit wait to synchronize with browser because browser is slower. But in this case, browser is faster and before command waits for the visibility , the element disappears hence failing the operation.
It would be great if anyone can help regarding the issue.
PS I am using jmeter webdriver plugin.
Thanks.
You could handle exception which breaks your validation (ignore NoSuchelementException but fail validation on TimeoutException) or create waiting method which waits for element to appear and after that wait to disappear.

Selenium webdriver wait for every action

Can I make selenium webdriver to wait for its every action execution "by default"? For example, set any "default wait time" to make it try to click every element 10 times every 500 ms?
I found it quite fussy for wait times, ended up installing webdriverIO to work with it, you can set timeout with that.
Theres this post about the different imeouts if its any use:
https://sqa.stackexchange.com/questions/2606/what-is-seleniums-default-timeout-for-page-loading