如何在 Selenium 中执行浏览器导航?
software testingautomation testingselenium web driver
Selenium 提供了多种 `navigate()` 方法来实现浏览器导航。它们如下所示:
navigate().to(url)
此方法用于启动一个新的浏览器窗口,并打开参数中指定的 URL。
navigate().refresh()
此方法用于刷新页面。
navigate().back()
此方法用于根据浏览器历史记录跳转到上一页。
navigate().forward()
此方法用于根据浏览器历史记录跳转到下一页。
示例
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
public class BrowserNavigation {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.tutorialspoint.com/index.htm";
// 新浏览器将启动并导航至该URL。s
driver.navigate().to(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// 刷新当前浏览器
driver.navigate().refresh();
//在 CSS 表达式中使用 id 标签名属性组合
driver.findElement(By.cssSelector("input[name=’search’]")).
sendKeys("Selenium");
//浏览器将返回上一页
driver.navigate().back();
//浏览器将跳转到下一页
driver.navigate().forward();
driver.close();
}
}

