PHPUnit and WebDriver(Selenium2) - How to operate an element before the page's fully loaded? - selenium

By default , Webdriver always wait for a page's full loading, then find the element on this page and operate it.
e.g. open the google.com website, wait for its full loading, then find the search button ,and click it.
Sometimes, the page connects to some external link ,such as GA (statistics service), which usually takes too much time. And the button that I want to click has been loaded before the page's completely loaded.
How can I let Webdriver click a button when the button has been loaded ,when other parts of the page are still in loading progress?

You could always put a wait command in there.
sleep(5);
This will make it ' sleep for 5 seconds '
Though, it's easier to use ' waitForElementPresent();
$this->waitForElementPresent("YOURELEMENT","50000");
this will make your script wait for 50 seconds or until the element is loaded
YOURELEMENT = the locator
50000 = The timeout

Related

Selenium command taking more time than specified in wait

I have a selenium test to navigate to a login page. Enter user name and password and click the login button. Once logged in, check whether an element is present in the home page.
I am keeping track of the time each command takes
WebDriver driver = driver.manage().timeouts().pageLoadTimeout(30L, TimeUnit.SECONDS);
driver.get("<url>");
WebDriverWait webDriverWait = new WebDriverWait(driver, 20);
Even though my wait is for 20 seconds, When I check the total time for this command to execute it can take up to 30 (this happens on failed logins). I am trying to figure out why selenium is taking 30 seconds instead of 20 in case of failures? I read through the documentation etc but no where did I see that pageLoadTimeout impacts loading when a button is clicked.
It seems like the driver.get("<url>"); command takes some time.
Means the page needs some time to load. You could load the page threaded tho.

Robotframework - Selenium - How to verify page loaded completely (successfully) without relying on a specific element in the page?

I was wondering if there is anything in Selenium that can verify if a page loaded completely without relying on a specific element in the page to appear (while using the "wait until page contains" or similar keywords).
The idea is reusability. If there is a need to add new websites to the robot automation later, I do not want to rely on a specific element that might exist in one page but not in another to verify if the page loaded fully.
Is there any keyword that addresses that in Robot Framework - Selenium Library?
Thanks!
Edit - I am aware that some AJAX requests are impossible or extremely difficult to conclude if they finished or not (or if they just keep going forever) so let's assume the website does not have any of that.
Once the page is finished loading, all requests are done.
driver.execute_script("return document.readyState === 'complete'")
use execute script document.readyState , if it returns complete it means the loading have finiished.
But if there is no ajax you don't have to do it selenium does it automatically so your question won't be valid:
https://www.selenium.dev/documentation/en/webdriver/page_loading_strategy/#:~:text=Defines%20the%20current%20session's%20page,loading%20takes%20lot%20of%20time.
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.
I couldn't find some built-in option but there is another way to achieve this. Check if your web app uses any third-party library which shows progress bar, if yes then probably there is function which returns the status of page loading including Ajax call. If not, then you can ask devs to add some progress bar, for instance Pace.JS
document.readyState does not wait for Ajax calls, personally I found it less helpful.

Page Title is not captured correctly in selenium webdriver

consider i am having two pages, first page -Login page and second Page -welcome page. If i provide the user credential in the login page, then it will be redirected into the welcome page.
I need to verify the page title in both the pages. so, i have used driver.getTitle(); after the page navigation steps. But, it is displaying the first page title and second page tile is not displaying
Thanks
Subbu
It's most like a timing issue - you are getting the title while the second page is not loaded yet. Use Explicit Wait via WebDriverWait class to wait for the second/welcome page to load (sample).
You may even use "title" specific wait conditions, see:
titleIs
titleContains
you must add explicit wait and check whether second page is loading in the given time or not.

How to identify element for notifications that disappear within few seconds?

I've a test case where after creation of an item, notification is shown, but that notification disappear s within 2-3 seconds.
I want to identify the element for that notification, but when I try to inspect element for that in firebug, it's HTML snippet disappears very quickly since notification itself disappears. Hence I couldn't identify element for it and finding it very difficult to automate.
Can anyone suggest how to deal with such situation?
Since you say "I've a test case", I'm thinking you are testing an application of yours. The plainest way to get around your problem is to know your application. Even if a third-party library is providing the notification code, you can read the doc to see if you can increase the delay or you can read the source code to figure out how it creates the element and where.
If the above fails, then if you can get together a sequence of operations in Selenium that triggers a notification, you should be able to get a serialization of body quickly enough that you can then examine at your leisure. Using Selenium for Python, it would go:
print driver.execute_script("return document.body.outerHTML;")
I would run the code above with redirection to save the output of the print statement to a file that I'd then examine at my leisure. You could make it narrower if you wish by getting the outerHTML of a descendant of body. I like to have a good bit of context so that I know where exactly the element is being created. I've used libraries and configurations that create such notifications as children of body, and some that put them as children of other elements.
Open the previous page before the notification
Press "Ctrl+Shift+c", Navigate to "sources" tab
Do the manual step to generate the notification, which is going to hide in few seconds
Press "F8". Page load scripts will be paused
Then inspect the element as usual and fetch the xpath at your convenience
You can use the below JAVA code to wait for 20 seconds till any element with text 'your notification' appears in the page:
try{
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'Your notification here')]")));
}catch(Throwable e){
System.err.println("Error while waiting for the notification to appear: "+ e.getMessage());
}

IEDriver Wait Loading page

I am using selenium IEDriverServer along with python.
Is it possible to make sure that IEDriverServer waits for a page to load , until some time and if the operation is not complete in time then it stops loading the page and we can proceed to next step.
Actually i am using a test page which contains takes too much time to load. All i am trying to do is to let it wait for the page to load until 10 sec and if it is unable to load in that time then it stops loading that page and moves over to the next step.
Can anyone suggest how this can be achieved in selenium
Webdriver has both implicit waits as well as explicit waits.
Implicit means it implicitly waits for the specified time on all actions.
driver.implicitly_wait(10)
Explicit means you can specify to wait for an expected condition to be met, which can be set for a page or some specific element
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))
Documentation here