• Selenium Video Tutorials

Selenium WebDriver - 处理链接



Selenium Webdriver 可用于处理网页上的链接。在HTML术语中,每个链接(称为超链接)都由名为 anchor 的标签名标识。此外,网页上的每个链接都有一个名为 href 的属性。

HTML 中链接的标识

现在让我们讨论一下如何识别网页上显示的超链接的锚标签 - 下图所示。右键单击网页,然后单击 Chrome 浏览器中的“检查”按钮。之后,整个页面的相应 HTML 代码将可见。要检查页面上的“Created”链接,请单击下面突出显示的左上箭头。

Selenium Handling 1

单击并指向“Created”超链接后,其 HTML 代码可见。

Selenium Handling 2

可以使用 Selenium 中的链接文本定位器来识别链接。将识别具有匹配链接文本值的第一个元素。

语法

Webdriver driver = new ChromeDriver(); driver.findElement(By.linkText("value of link text"));

使用链接文本定位器处理链接

让我们以上面页面为例,单击“Created”链接后,页面上将显示文本Link has responded with status 201 and status text Created

Selenium Handling 3

示例

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 HandLinks { 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); // Opening the webpage where we will identify an element driver.get ("https://tutorialspoint.com/selenium/practice/links.php"); // identify link with link text locator then click WebElement l = driver.findElement(By.linkText("Created")); l.click(); // identify text locator WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]")); System.out.println("Text appeared is: " + t.getText()); // Closing browser driver.quit(); } }

输出

Text appeared is: Link has responded with status 201 and status text Created

Process finished with exit code 0

在上面的示例中,单击链接Created后获得的带有消息的文本为Link has responded with status 201 and status text Created

最后,收到消息Process finished with exit code 0,表示代码已成功执行。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

使用部分链接文本定位器处理链接

可以使用 Selenium 中的部分链接文本定位器来识别链接。将识别具有匹配部分链接文本值的第一个元素。

语法

Webdriver driver = new ChromeDriver(); driver.findElement(By.partialLinkText("value of partial link text"));

示例

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 HandPartialLinks { 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); // Opening the webpage where we will identify an element driver.get("https://tutorialspoint.com/selenium/practice/links.php"); // identify link with partial link text locator then click WebElement l = driver.findElement(By.partialLinkText("Creat")); l.click(); // identify text locator WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]")); System.out.println("Text appeared is: " + t.getText()); // Closing browser driver.quit(); } }

输出

Text appeared is: Link has responded with status 201 and status text Created

在上面的示例中,单击链接Created(使用部分链接文本)后获得的带有消息的文本为链接Link has responded with status 201 and status text Created

使用标签名定位器处理链接

可以使用 Selenium 中的标签名定位器来识别链接。将识别具有匹配标签名值的第一个元素。

在上例中,我们单击并指向“Created”超链接后,其 HTML 代码可见,反映了锚标签名(称为“a”并用<>括起来)。

<a href="javascript:void(0);" id="created" onclick="shide('create')">Created</a>

语法

Webdriver driver = new ChromeDriver(); driver.findElement(By.tagName("a”));

让我们以同一个页面为例,我们首先计算链接的总数,然后单击特定链接,例如“No Content”。单击该链接后,我们将获得文本Link has responded with status 204 and status text

Selenium Handling 4

示例

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; import java.util.List; public class TotalLinks { 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); // Opening the webpage where we will identify an element driver.get("https://tutorialspoint.com/selenium/practice/links.php"); // identify link with link text locator then click WebElement l = driver.findElement(By.linkText("No Content")); l.click(); // Retrieve all links using locator By.tagName and storing in List List<WebElement> totalLnks = driver.findElements(By.tagName("a") ); System.out.println( "Total number of links: " + totalLnks.size() ) ; // Running loop through list of web elements for( int j = 0; j < totalLnks.size(); j ++){ if( totalLnks.get(j).getText().equalsIgnoreCase("No Content") ) { totalLnks.get(j).click(); WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[2]")); // get the browser title to confirm navigation after click System.out.println( "Get text after click: " + t.getText()); break ; } } // Closing browser driver.quit(); } }

输出

Total number of links: 42
Get text after click: Link has responded with status 204 and status text No Content

在上面的示例中,我们计算了网页上链接的总数,并在控制台中收到了消息 - Total number of links: 42 和单击后获得的文本 Get text after click: Link has responded with status 204 and status text No Content

结论

本教程总结了 Selenium Webdriver 处理链接的全面内容。我们首先介绍了如何在 HTML 中识别链接,并通过示例说明了如何使用 Selenium Webdriver 中的链接文本、部分链接文本和标签名定位器来处理链接。这使您掌握了 Selenium Webdriver 处理链接的深入知识。最好不断练习所学内容,并探索与 Selenium 相关的其他内容,以加深您的理解并拓宽视野。

广告