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.
Related
I am using Selenium Chrome Driver to test my application.
There is one page in the Browser where on Load an Alert message appears and I have to Click OK.
The problem is when I try to use driver.SwitchTo().Alert in my code. The Alert appears on the foreground page while the page is still loading. When I continue to debug at this point, my driver waits for an infinite time, and when I click manually on OK button, then it tries to switchto().alert, but since there is no Alert, it fails.
I would appreciate any help on this.
Note: The Page is in Loading Form till I click on Ok button on Alert , I wonder if it is alert
I also followed the below solution , but it does not work for me
https://groups.google.com/forum/#!topic/selenium-users/CixorzKZE4E
I get the following exception ,
he HTTP request to the remote WebDriver server for URL localhost:3200/session/0285afd8049f70878988405463448d24/… timed out after 60 seconds.
I can still see alert on my child window .
you could use explicit wait for the loading part.ie if webdriver instance is driver
WebDriverWait wait=new WebDriverWait(driver, //mention the time as per need here ie 20);
wait.until(ExpectedConditions.urlToBe("mention the url"));
before the alert handling code.
you need to accept or dismiss the alert, code you can try :
driver.switchTo().alert().accept();
I find out that the alert was a Javascript alert and how to handle javascript alerts is mentioned in Selenium Documentation.
I'm trying to test my Hotmail account using selenium Webdriver 3.0. I set my account to ask for two way authentication, which means Indeed to enter the last 4 digits of my mobile number and then send a message to me. Then I have to enter that code in order to open my email Account.
It worked good with me when I used implicit, waited for 60 seconds until i receive the code then enter it manually and so the test continues to my email page >> all works fine.
BUT my question is, is there any way to make the test wait until I enter the code rather than waiting for 60s?!
Is that acceptable to enter some things manually while using Selenium webdriver?
is that acceptable to enter some things manually while using selenium webdriver?
Yes, because this thing is made for stopping robot activity just like Captcha code entering the process. So, in this scenario, this is acceptable to enter text manually after reading the text from mail while using selenium.
You can do one thing more if you want to do it automatically, you should implement mail API to read the last mail from your provided account in the background and fetch necessary text from the last mail using some programming stuff and enter it into textbox using selenium.
is there any way to make the test wait until I enter the code rather than waiting for 60s?
Yes, to achieve this you need to create your own custom ExpectedConditions with WebDriverWait which will wait until located text box has value greater or equal 4 character or other suitable condition which you want as below :-
//Initialize WebDriverWait first which will wait maximum 60 seconds
WebDriverWait wait = new WebDriverWait(driver, 60);
//Create suitable locator to locate textbox element eg. with xpath locator
By byObject = By.xpath("enter here textbox xpath");
//This condition will wait until text box has value greater or equal 4 character
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return (d.findElement(byObject).getAttribute ("value").length() >= 4)
}
});
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
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
Previously I have been using chrome Auto Refresh plug in. However, now my code has multiple ChromeDriver instances opening and closing and I cannot use Auto Refresh. Also, it is quite a hassle to install Auto Refresh on new computers.
Is there any way to refresh driver (simulate F5 say every 15 seconds if driver does not change remains motionless) with Selenium similar to Google Auto Refresh?
refresh is a built in command.
driver = webdriver.Chrome()
driver.get("http://www.google.com")
driver.refresh()
If you don't have the chrome driver it can be found here:
https://code.google.com/p/chromedriver/downloads/list
Put the binary in the same folder as the python script you're writing. (Or add it to the path or whatever, more information here: https://code.google.com/p/selenium/wiki/ChromeDriver)
edit:
If you want to refresh ever 10 seconds or something, just wrap the refresh line with a loop and a delay. For example:
import time
while(True):
driver.refresh()
time.sleep(refresh_time_in_seconds)
If you only want to refresh if the page hasn't changed in the meantime, keep track of the page that you're on. driver.current_url is the url of the current page. So putting it all together it would be:
import time
refresh_time_in_seconds = 15
driver = webdriver.Chrome()
driver.get("http://www.google.com")
url = driver.current_url
while(True):
if url == driver.current_url:
driver.refresh()
url = driver.current_url
time.sleep(refresh_time_in_seconds)
Well there are two ways of doing this.
1. We can use refresh method
driver.get("some website url");
driver.navigate().refresh();
We can use actions class and mimic F5 press
Actions act = new Actions(driver);
act.SendKeys(Keys.F5).perform();
If you write unit tests that must be run like if you had to open/refresh a new browser session each time, you can use a method with before annotations:
#Before
public void refreshPage() {
driver.navigate().refresh();
}
If all tests are individually successful (green) but fail all together, the reason might also been that you need to wait for some resources to be available on the page, so you also need to handle it, setting the timeout like this:
public WebElement getSaveButton() {
return findDynamicElementByXPath(By.xpath("//*[#id=\"form:btnSave\"]"), 320);
}
320 is a long time, but you must make sure that you give enough time to get all that it takes to test.