112.md 6.3 KB
Newer Older
W
wizardforcel 已提交
1
# 9k。 WebDriver – 定位元素:第 2 部分(按 className,linkText,partialLinkText)
W
init  
wizardforcel 已提交
2 3 4 5 6 7 8 9 10

> 原文: [https://javabeginnerstutorial.com/selenium/9k-webdriver-locating-elements-2/](https://javabeginnerstutorial.com/selenium/9k-webdriver-locating-elements-2/)

朋友! 让我们今天更深入地研究一下定位元素的更多策略。 在这篇文章中,我们将重点放在

*   班级名称
*   LinkText
*   partialLinkText

W
wizardforcel 已提交
11
### 按类别名称定位
W
init  
wizardforcel 已提交
12

W
wizardforcel 已提交
13
类名称不过是用于设置 Web 元素样式的 CSS 类名称。 重要的是要注意页面上的许多 Web 元素可能具有相同的 className。 在这种情况下,可以使用 findElements 方法,并且可以为结果建立索引。 请参考通过 tagName 策略定位(解释以及[先前文章](https://javabeginnerstutorial.com/selenium/9j-webdriver-locating-elements-1/)中提供的示例)。 如果我们有一个具有唯一 className 的元素,或者被测元素是该页面中使用该 className 的第一个元素,则 findElement 将执行此任务。
W
init  
wizardforcel 已提交
14

W
wizardforcel 已提交
15
***语法*** *driver.findElement(By.className(“ element_class_name”));*
W
init  
wizardforcel 已提交
16

W
wizardforcel 已提交
17
***说明*** 找到具有匹配 CSS 类名称的第一个元素。
W
init  
wizardforcel 已提交
18

W
wizardforcel 已提交
19
***示例*** 让我们找到 gmail 帐户创建页面的“手机”文本框。
W
init  
wizardforcel 已提交
20

W
wizardforcel 已提交
21
右键单击文本框,然后选择检查元素以获取相应的 HTML 代码。 我们可以看到“ input”标签包含 *class =“ i18n_phone_number_input-inner_input”* 。 让我们继续使用此类名称查找“手机”文本框,以进行进一步的交互。
W
init  
wizardforcel 已提交
22 23 24 25 26 27 28 29 30

*代码:*

```java
driver.findElement(By.className("i18n_phone_number_input-inner_input"));
```

![Locating by ClassName](img/35fbb3061abb7c05aa82266e0c5f1ebf.png)

W
wizardforcel 已提交
31
### 通过 linkText 定位
W
init  
wizardforcel 已提交
32

W
wizardforcel 已提交
33
当您想与超链接进行交互时,linkText 非常有用。 使用该链接在网页上显示的实际文本。 那有多容易?
W
init  
wizardforcel 已提交
34

W
wizardforcel 已提交
35
***语法*** *driver.findElement(By.linkText(“ hyperlink_text”));*
W
init  
wizardforcel 已提交
36

W
wizardforcel 已提交
37
***说明*** 找到具有匹配链接文本的第一个超链接。
W
init  
wizardforcel 已提交
38

W
wizardforcel 已提交
39
***示例*** 让我们找到在 gmail 帐户创建页面底部提供的超链接“了解更多信息”。
W
init  
wizardforcel 已提交
40 41 42 43 44 45 46 47 48

*Code:*

```java
driver.findElement(By.linkText("Learn more"));
```

![Locating by LinkText](img/1a2edf43e534f498a4f3275dd5f6e4f3.png)

W
wizardforcel 已提交
49
### 通过 partialLinkText 定位
W
init  
wizardforcel 已提交
50

W
wizardforcel 已提交
51
PartialLinkText 也用于与超链接进行交互,与 linkText 定位策略非常相似。 此方法不提供部分完整链接,而是提供链接显示的完整文本。 因此,可以将链接文本的一部分作为匹配条件。
W
init  
wizardforcel 已提交
52

W
wizardforcel 已提交
53
***语法*** *driver.findElement(By.partialLinkText(“ hyperlink_partial_text”)));*
W
init  
wizardforcel 已提交
54

W
wizardforcel 已提交
55
***说明*** 找到第一个超链接,其中**包含**指定的部分链接文本。
W
init  
wizardforcel 已提交
56

W
wizardforcel 已提交
57
***示例*** 让我们在 Gmail 帐户创建页面的“选择用户名”文本框下方找到超链接“我更喜欢使用我当前的电子邮件地址” -text,**'我更喜欢'**
W
init  
wizardforcel 已提交
58 59 60 61 62 63 64 65 66

*Code:*

```java
driver.findElement(By.partialLinkText("I prefer to"));
```

![Locating by partialLinkText](img/6a482332c4ee93dfee4745bc0fa27f77.png)

W
wizardforcel 已提交
67
## 总体图片
W
init  
wizardforcel 已提交
68 69 70

让我们看一个实现上述三种定位器类型的测试用例。

W
wizardforcel 已提交
71
#### 场景
W
init  
wizardforcel 已提交
72

W
wizardforcel 已提交
73 74 75
1.  打开 Firefox 浏览器。
2.  导航到 Google 帐户创建页面
3.  通过 className 找到手机文本框
W
init  
wizardforcel 已提交
76
4.  输入“ 9496543210”作为手机号码
W
wizardforcel 已提交
77
5.  通过 linkText 找到“了解详情”超链接
W
init  
wizardforcel 已提交
78
6.  链接超链接
W
wizardforcel 已提交
79
7.  找到“我更喜欢使用当前的电子邮件地址”超链接和 partialLinkText
W
init  
wizardforcel 已提交
80 81
8.  将完整的链接文本打印到控制台进行验证

W
wizardforcel 已提交
82
此方案的 JUnit 代码是,
W
init  
wizardforcel 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

```java
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementLocatorTest2 {
		//Declaring variables
		private WebDriver driver; 
		private String baseUrl;

		@Before
		public void setUp() throws Exception{
			// Selenium version 3 beta releases require system property set up
			System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\"
					+ "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
			// Create a new instance for the class FirefoxDriver
			// that implements WebDriver interface
			driver = new FirefoxDriver();
			// Implicit wait for 5 seconds
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			// Assign the URL to be invoked to a String variable
			baseUrl = "https://accounts.google.com/SignUp";
		}

		@Test
		public void testPageTitle() throws Exception{
			// Open baseUrl in Firefox browser window
			driver.get(baseUrl);
			// Locate Mobile phone text box by className and
			// assign it to a variable of type WebElement
			WebElement mobileNum = driver.findElement(By.className("i18n_phone_number_input-inner_input"));
			// Clear the default placeholder or any value present
			mobileNum.clear();
			// Enter/type the value to the text box
			mobileNum.sendKeys("9496543210");
			// Locate 'Learn more' hyperlink by link text
			WebElement link1 = driver.findElement(By.linkText("Learn more"));
			// Click on 'Learn more'
			link1.click();
			// Locate hyperlink by partial link text
			WebElement link2 = driver.findElement(By.partialLinkText("I prefer to"));
			// Printing the complete link text to console
			System.out.println("Complete link text: " + link2.getText());
		}

		 @After
		  public void tearDown() throws Exception{
			// Close the Firefox browser
			driver.close();
		}
}
```

W
wizardforcel 已提交
142
*执行结果*
W
init  
wizardforcel 已提交
143 144 145 146 147

注释清楚地提供给每一行代码,因此很容易解释。

![Console output](img/ba6b69d34397c93cbfa5d3246cc4c6bb.png)

W
wizardforcel 已提交
148
在 JUnit 窗格中,绿色条显示测试用例已成功执行。 输出将打印到控制台,以确认仅提供子文本“我更喜欢”作为 partialLinkText,即可访问“我更喜欢使用当前的电子邮件地址”超链接。
W
init  
wizardforcel 已提交
149 150 151

![Output](img/ec0d674929c66c5314ee6de72154ea7c.png)

W
wizardforcel 已提交
152
该图像的左半部分显示输入的电话号码,右半部分显示在 Firefox 浏览器中执行的最终输出。 点击“了解详情”链接后,我们将重定向到相应的页面。
W
init  
wizardforcel 已提交
153 154 155 156 157

是时候再休息一次了。 在接下来的文章中,准备好消化更多的信息,因为我们将研究两种有效的元素定位技术。

享受这一天!