How to stop page load in Selenium Webdriver - selenium

I have logged into an account on one of the websites. After login, a click on a button presents that page, but the page keeps on loading without displaying the next page.
What I wanted is, if the page keeps on loading without giving any response for some time, I need to stop the execution of the script.
I have tried by using the below code, but it didn't work
driver.manage().timeouts().pageLoadTimeout(500, TimeUnit.MILLISECONDS);

If you are using firefox, use firefox profile with selenium and set network.http.connection-timeout to seconds (for ex I am setting it to 10 secs) -
In Java,
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.connection-timeout", 10);

For stopping the page-load you can use this
public void stopPageLoading() {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return window.stop");
}

Related

Link Click using selenium webdriver is giving timeout error, link is working fine on browser otherwise

I'm trying to write a piece of code using Testng in selenium, the problem is when i try to click a link, the code becomes unresponsive and gives error- Timed out receiving message from renderer
Tried driver.get("https://infostore.saiglobal.com/") instead of driver.findElement(By.linkText("Infostore")).click(); still remains unresponsive - Doesnot get directed to the webpage - https://infostore.saiglobal.com/
#Test
public void testSai() throws Exception {
driver.get("https://www.saiglobal.com/"); //open homepage
driver.findElement(By.id("CybotCookiebotDialogBodyButtonAccept")).click(); //accept cookie
driver.findElement(By.cssSelector("#sai-header > div > div.main-nav > div.store-box > div")).click(); //click on Login to open list
sleep(2);
driver.findElement(By.linkText("Infostore")).click(); //click on infostore to be redirected to https://infostore.saiglobal.com/
System.out.println(driver.getCurrentUrl());
Yes I have checked this issue and there is an workaround for this issue if you wish to have this.Try following code.
Take the href attribute value from the link element.
Delete all cookies.
driver.navigate().to(URL);
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + File.separator + "\\Executables\\Chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 5);
Actions actions = new Actions(driver);
driver.get("https://www.saiglobal.com/");
driver.findElement(By.id("CybotCookiebotDialogBodyButtonAccept")).click();
WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='desktop-login']")));
actions.moveToElement(element).build().perform();
WebElement element1=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='desktop-login']/ul/li/a[contains(text(),'Infostore')]")));
String str1=element1.getAttribute("href");
driver.manage().deleteAllCookies();
driver.navigate().to(str1);
System.out.println(driver.getCurrentUrl());
}
This seems to be an error with Selenium that the developers have been dealing with for over a year. They never really give a concrete answer as to what causes it or how to fix it.
The first port of call would be to make sure that your browser and Selenium are compatible. Try opening a different URL that you know works, and if the error persists then there is likely an error with the compatibility of your selenium and web browser. If it works, then there is something wrong with your website.
For reference, I opened your website using Python Selenium, and had no issues loading or interacting with elements. So the error is local to the software you are using.
The issue can also be caused by sleeps(no idea why), so try removing any sleeps and see if this stops the issue.

Getting 500 error page on logout by Selenium Webdriver script

Updated with code
driver.get("https://stage.ab.org/");
System.out.println(driver.getTitle());
System.out.println(driver.getPageSource());
driver.findElement(By.id("txt-username")).sendKeys("Username");
driver.findElement(By.id("pwd-password")).sendKeys("Passw0rd");
driver.findElement(By.id("login-widget-submit")).click();
Thread.sleep(2000);
driver.findElement(By.cssSelector(".username-link-container")).click();
Thread.sleep(8000);
WebDriverWait wait = new WebDriverWait(driver, 9);
WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.id("alinkSignOut")));
link.click();
I do not get any errors in Eclipse.
When the Webdriver script runs (logging out a user) it gives 500 Error page. When I do it manually, I am able to it successfully. What is going on with Selenium?
Some issues with VPN, working fine now

Selenium in multi-step iframes (paypal)

I want to test paypal payment with selenium/selenide.
I am waiting for the embedded frame to be loaded, then switch to it, fill out the login form and click the login button.
but how can I wait from there for the next step to be loaded? (there is a significant load-time between the steps that happens after the iframe was initially loaded, so the waitForLoad doesn't catch it)
I have tried waitForLoad(confirmButton), but it timeouts. (I'm still in the iframe focus).
how can I do this?
thanks
Use wait command:
public void waitForElementPresent() {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions
.visibilityOfElementLocated(By.id("");
}

SafarDriver Selenium Page title is incorrect after loading new page

I have a Selenium 2 test that uses a login form and then asserts that the login has succeeded by verifying the page title has changed.
The test is written in Java/JUnit, WebDriver and Fluent Selenium
fwd.title().within(secs(5)).shouldContain(partialTitle);
The tests work on Firefox and Chrome.
On Safari the page title is still the old login page title.
The screencast we have generated shows that the URL after a successful login has changed, but the title hasn't.
I think the SafariDriver is not waiting for the page load event to complete before the assertion.
I have the same issue!!!
My test fail because title is used for checking test "Step".
With Firefox and Chrome no problem; with Safari it's a problem!!!
I found this "hack" that work, but it's necessary to manually check the title after each "click":
def static void waitForPage(driver, String title) {
//Hack to make login working on Safari&IE
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().startsWith(title);
}
});
}
I use GEB + SPOCK and with safari driver there is a "know issue" about implicit/explicit wait (ref. https://code.google.com/p/selenium/issues/detail?id=5860).
So, a nice workaround for make test runnable on Safari browser is to wait for page title:
static at = {
waitFor { title == "Homepage" }
}
This will force the driver to wait until title change (the timeout is configurable).

Selenium Force Close Firefox

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ff = profile.getProfile("ScreenCapture");
WebDriver driver = new FirefoxDriver(ff);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get(url);
Thread.sleep(8000);
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
driver.quit();
Shouldn't driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); force a close of the selenium generated Firefox browser after 15 seconds? The browser just sits and says its transferring data for an hour+. Basically just hangs saying its transferring...
I am capturing ajax heavy pages which is why Im asking everything to wait for 8 seconds after page loads. But that should have nothing to do with the driver forcing a close after 15 seconds.
Where am I going wrong?
Details: Centos x64 6.4 with Firefox 10.0.12 and latest Selenium as of 10 min ago.
Is there something I can do in Java to go around this?
Question: How can I force close the Selenium generated Firefox window after N seconds?
If you use Junit along with Java, then some thing like this :-
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
Note :-
To get a full skeleton of how it should be written just download the selenium IDE for FF and export some test case to Java /jUnit.
My linux knowledge is limited, but you can kill a process by running the linux command pkill.
driver.quit();
Thread.sleep(15000); //use a poll loop instead to check process running every 1 sec
Runtime rt = Runtime.getRuntime();
rt.exec("pkill firefox");
I think that the java process will need to have enough permissions to kill a process, but haven't tried it.
To follow up on Ardesco's comment, an example would look as follows:
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ff = profile.getProfile("ScreenCapture");
WebDriver driver = new FirefoxDriver(ff);
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
try {
driver.get(url);
} catch (TimeoutException e) {
System.out.println("15 seconds were over, force continue!");
} finally {
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
driver.quit();
}
The try part will run the request but when the timeout time set with pageLoadTimeout has been exceeded an exception is thrown which we catch. The finally part will be run regardless if the requested page was loaded properly in less than 15 seconds or whether an exception was thrown.
Implicit waits will not force a close of a browser after 15 seconds.
Implicit waits are used when trying to find elements in the DOM, they are not used when trying to load a page. If you want Selenium to stop trying to load a page after 15 seconds you will need to set a pageLoadTimeout, it's used like this:
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
The default page load timeout is 0 (which means wait indefinitely), hence the behaviour that you are seeing.
(There is obviously an assumption here that the driver binary you are using has implemented this method.)
The JavaDoc for timeouts in Selenium is available Here