如何使用 WebDriver JavaScript Executor 通过 Postman 导航至某个 URL?
我们可以使用 Selenium webdriver JavaScript Executor 来导航到某个 URL。Selenium 可以通过使用 executeScript 方法来运行 JavaScript 命令。
传递给 executeScript 方法以导航至某个 URL 的参数是 - window.location = \'"+s+"\'。此处,s 是存储待导航页面的链接的变量。
语法
JavascriptExecutor js = (JavascriptExecutor) driver; String s = "https://tutorialspoint.com/about/about_careers.htm"; String scrpt = "window.location = \'"+s+"\'"; js.executeScript(scrpt);
代码实现
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class LnkJSNavigate{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java \chromedriver.exe"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS); // Javascript Executor to open a link JavascriptExecutor js = (JavascriptExecutor) driver; String s = "https://tutorialspoint.com/about/index.htm"; String scrpt = "window.location = \'"+s+"\'"; js.executeScript(scrpt); System.out.println("Page navigated to:" + driver.getTitle()); driver.close(); } }
输出
广告