程序清单5-12 Java代码
package Project1;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver.*;
import org.openqa.selenium.firefox.*;
public class Project1Class {
public static void main(String[] args) {
//如果启动出现问题,可以使用System.setProperty指出firefox.exe的路径
//System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com");
navigation.to("http://www.google.com.hk");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
navigation.back();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
navigation.forward();
}
}
程序清单代码执行后可以发现,程序共打开了两个页面:百度和谷歌。然后,页面先后退到了第一个页面(百度),再前进到了第二个页面(谷歌)。
5.4.3 Refresh()
使用该方法将刷新整个页面(类似于按F5的效果),多用于执行某些操作后需要刷新的情况(例如登录后页面未自动刷新),代码如程序清单5-13或程序清单5-14所示。
程序清单5-13 C#代码
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
INavigation navigation = driver.Navigate();
navigation.GoToUrl("http://www.baidu.com");
navigation.Refresh();
}
}
}
程序清单5-14 Java代码
package Project1; |