Selenium 简明教程
Selenium WebDriver - Browser Commands
Selenium Webdriver 提供了多个命令,这些命令有助于打开浏览器、对打开的浏览器执行一些操作并退出浏览器。
Basic Browser Commands in Selenium Webdriver
下面讨论了一些浏览器命令−
How to Inspect Elements on a Web Page?
首先在网页上单击鼠标右键,然后在 Chrome 浏览器中单击“检查”按钮。这之后会打开整个页面的 HTML 代码。若要调查页面上的特定元素,请单击可见 HTML 代码顶部的左上方箭头,如下所示。
Example 1
让我们以以下页面为例,首先将通过 URL 启动一个应用程序: Selenium Automation Practice Form ,然后获取浏览器标题 Selenium Practice - Student Registration Form 。
请注意,我们可以在 head 标签内部的 title 标签的 HTML 代码中获取网页的浏览器标题。在下图中,我们看到该页面的标题为 Selenium Practice - Student Registration Form 。
然后,我们将单击 Login 按钮,之后会导航到另一个页面的浏览器标题 Selenium Practice - Login 和 URL 为: Selenium Automation Practice Form 。最后,我们将退出 Web 驱动程序会话。
代码实现
package org.example;
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;
public class BrowserCommand {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// launching a browser and open a URL
driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");
// Getting browser title after launch
System.out.println("Getting browser title after launch: " + driver.getTitle());
// Getting browser URL after launch
System.out.println("Getting URL after launch: " + driver.getCurrentUrl());
// identify link then click
WebElement l = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a"));
l.click();
// Getting browser title after clicking link
System.out.println("Getting browser title after clicking link: " + driver.getTitle());
// Getting browser URL after launch
System.out.println("Getting URL after clicking link: " + driver.getCurrentUrl());
// Quitting browser
driver.quit();
}
}
Output
Getting browser title after launch: Selenium Practice - Student Registration Form
Getting URL after launch:
https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php
Getting browser title after clicking link: Selenium Practice - Login
Getting URL after clicking link:
https://www.tutorialspoint.com/selenium/practice/login.php
Process finished with exit code 0
在上述示例中,我们已经打开了浏览器中的一个 URL,并通过控制台中的消息分别获取了浏览器标题和当前 URL - Getting browser title after launch: Selenium Practice - Student Registration Form and Getting URL after launch: Selenium Automation Practice Form 。
然后,我们已经单击了登录并在导航后通过控制台中的消息收到了浏览器标题和当前 URL - Getting browser title after clicking link: Selenium Practice - Login and Getting URL after clicking link: Selenium Automation Practice Form 。接下来,我们必须退出驱动程序会话。
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Example 2
让我们以以下图像所示的另一个示例为例,我们将在其中单击 New Tab 。
之后,我们将获得另一个包含文本 New Tab 的窗口。
然后,我们将关闭文本 New Tab 的新窗口,返回到原始窗口,并在那里访问文本 - Browser Windows 。最后,我们将退出会话。
代码实现
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class WindowClose {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage where we will open a new window
driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php");
// click button and navigate to next window
WebElement b = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/button[1]"));
b.click();
// Get the window handle of the original window
String oW = driver.getWindowHandle();
// get all opened windows handle ids
Set<String> windows = driver.getWindowHandles();
// Iterating through all window handles
for (String w : windows) {
if(!oW.equalsIgnoreCase(w)) {
// switching to child window
driver.switchTo().window(w);
// accessing element in new window
WebElement e = driver.findElement
(By.xpath("/html/body/main/div/div/h1"));
System.out.println("Text in new window is: " + e.getText());
// closing new window
driver.close();
break;
}
}
// switching to parent window
driver.switchTo().window(oW);
// accessing element in parent window
WebElement e1 = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text in parent window is: " + e1.getText());
// quitting the browser session
driver.quit();
}
}
Example 3
让我们以上述页面的另一个示例为例,我们将在其中使用 getPageSource() 方法获取页面源代码。
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class WindowPagSource {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage where we will open a new window
driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php");
// Getting browser page source
System.out.println("Getting page source: " + driver.getPageSource());
// quitting the browser session
driver.quit();
}
}
在上述示例中,我们通过控制台中消息捕获了网页上启动的网页的页面源代码。