행위

"Selenium xpath"의 두 판 사이의 차이

DB CAFE

(XPath)
29번째 줄: 29번째 줄:
 
# XPath를 찾는 데 사용할 수있는 여러 도구가 있습니다. Firefox 브라우저 Firebug 및 XPath Checker 추가 기능 및 Chrome XPath Helper 및 Toggle XPather의 경우.
 
# XPath를 찾는 데 사용할 수있는 여러 도구가 있습니다. Firefox 브라우저 Firebug 및 XPath Checker 추가 기능 및 Chrome XPath Helper 및 Toggle XPather의 경우.
  
XPath 표현식 :
+
=== XPath 표현식 ===
  
  
 
XPath expression generally defines a pattern in order to select a set of nodes. XPath uses path expression to select nodes or list of nodes from an XML/html document. This path can be of two type:
 
XPath expression generally defines a pattern in order to select a set of nodes. XPath uses path expression to select nodes or list of nodes from an XML/html document. This path can be of two type:
  
Absolute Path
+
==== 절대경로 (Absolute Path) ====
Relative Path
+
# 절대 경로는 계층 문서 내에서 노드를 선택하기 위해 모든 단일 노드를 지정하는 것을 의미합니다.
Absolute path means to specify every single node to select a node inside a hierarchical document. It always starts from root node. Here is an example:
+
# 항상 루트 노드에서 시작합니다.  
 
+
# 예시
 +
<source lang=xml>
 
/html/body/div[2]/div/div[4]/div/ul/li[3]/a/span
 
/html/body/div[2]/div/div[4]/div/ul/li[3]/a/span
 +
</source>
  
It is relatively faster then Relative path. But the XPath expression may be very lengthy. More over each of those nested relationships will need to be present 100% of the time, or the locator will not work. For these reasons use of Relative XPath expressions are encouraged.
+
==== 상대경로 (Relative Path) ====
 
+
# 상XPath 표현식은 매우 길 수 있습니다.  
In Relative XPath expression you can start from the node of your choice. The above absolute path can be written in the following relative path:
+
# 중첩 된 각 관계에 대해 100% 존재해야 함. 그렇지 않으면 로케이터가 작동하지 않습니다.
 
+
#: 이러한 이유로 Relative XPath 표현식을 사용하는 것이 좋습니다.
 +
# 상대 XPath 표현식에서는 선택한 노드에서 시작할 수 있습니다.
 +
# 위의 절대 경로를 다음 상대 경로에도 쓸 수 있습니다.
 +
<source lang=xml>
 
//div[@id='navigation']/ul/li[3]/a/span
 
//div[@id='navigation']/ul/li[3]/a/span
 +
</source>
  
In this blog, my prime focus will be to demonstrate the Relative XPath.
+
==== 슬래쉬 “/” 와 "//" 의미 ====
Expression: “/” & “//
+
# XPath의 어느 곳에서나 하나의 슬래시 '/'는 상위 요소 내에서 바로 요소를 찾는 것을 의미합니다.  
You may notice from above two example that I sometime used “/” and sometimes “//. This is a very important point to understand. A single slash ‘/’ anywhere in XPath signifies to look for the element immediately inside its parent element. A double slash ‘//’ signifies to look for any child or any grand-child (descendant) element inside the parent element. In the following two example it will be more clear.
+
# 이중 슬래시‘//’는 상위 요소 내에서 하위 또는 손자 (하위) 요소를 찾는 것을 나타냅니다.  
 
+
# 두가지 예시 참조
 +
# 예시 1)
 +
<source lang=xml>
 
.//div[@id='products']/a/img
 
.//div[@id='products']/a/img
 
+
</source>
Look carefully in the above example, here I am trying to find an image tag from html document, which is the child of tag ‘a’. And tag a is the child of div tag. The same image tag can be found by:
+
## 'a' 태그의 하위 인 html 문서에서 이미지 태그를 찾으려고합니다.  
 
+
## 그리고 a 태그는 div 태그의 자식입니다.
 +
## 동일한 img 태그는 다음에서 찾을 수 있습니다.
 +
<source lang=xml>
 
.//div[@id='products']//img
 
.//div[@id='products']//img
 +
</source>
  
Here I am trying to find an image tag which is the descendant of tag div.
+
==== 점 “.” “..” 의미 ====
 
+
# "." 은 XPath에서 현재 노드를 지정하는 데 사용됩니다.
Expression: “.” & “..”
+
# ".." 은 문서 트리에서 한 단계 위로 이동하는 데 사용됩니다.  
You may have noticed that in every example I have used “.. This is used to specify current node in XPath.
+
#: 즉, 현재 노드의 부모를 찾습니다.  
..” is used to move one step up in the document tree, that is it will find the parent of current node. An example will make it more clear:
+
# 예시)
 
+
<source lang=xml>
 
.//div[@id='products']/..
 
.//div[@id='products']/..
 +
</source>
 +
* 위 표현식은 현재 노드의 부모 노드를 찾습니다.
  
This expression will find the parent node of the current node.
 
  
Expression: “@
+
==== "@”의 의미 ====
@” is used to select attribute in the document tree.
+
# "@"는 문서 트리에서 속성을 선택하는 데 사용됩니다.
In the above example I specify attribute id with “@” and the value products inside ‘ ’.
+
# 위의 예시에서 "@"로 속성 ID를 지정하고‘’안에 값 'products' 지정 함.
 
 
Expression: “*”
 
XPath support wildcard like “*”. Sometimes it is of much use to find a Relative path. Let’s see an example:
 
  
 +
==== “*”의 의미 ===
 +
# XPath는 "*"와 같은 와일드 카드를 지원합니다.
 +
#: 때로는 상대 경로를 찾는 데 많이 유용합니다.
 +
# 예시 )
 +
<source lang=xml>
 
.//*[text()='Home']
 
.//*[text()='Home']
 +
</source>
  
This wildcard can be used in place of attribute too.
+
<source lang=xml>
 
 
 
.//a[@*='Home']
 
.//a[@*='Home']
 +
</source>
 +
* 이 경우 값이 'home'인 속성을 가진 요소를 찾으려고합니다.
 +
* 이 경우 속성 이름은 신경 쓰지 않습니다.
  
In this case I am trying to find an element which has an attribute with value ‘home’. In this case I don’t care about the attribute name. You will not use it very often. But its good keep it as an option.
+
==== XPath 편리 팁 사용법 ====
Here in this section I am going to illustrate to most handy trick of XPath. Most of the times you will face issues when the locator’s properties are dynamically generated. And now a days petty much all the web application follows this strategy. Here comes the real challenge to deal with all the issue. Following section will give you concept about how to deal with such situations.
+
# 대부분의 경우 로케이터의 속성이 동적으로 생성 될 때 문제가 발생합니다.  
 
+
#:그리고 지금은 거의 모든 웹 애플리케이션이이 전략을 따르고 있습니다.  
Read the following section carefully and try to understand and implement these tricks.
+
===== “contains” 키워드 사용하는 표현식 ====
 
+
# "contains ()"는 속성이름과 속성 값 , 두 개의 인수를받는 함수입니다.  
Expression using keyword “contains”:
+
<source lang=xml>
“contains()” is a function that take two arguments, attribute name and attribute value. Let’s see an example:
 
 
 
 
.//div[contains(@id, 'search')]
 
.//div[contains(@id, 'search')]
 
+
</source>
Here I am finding a div tag which contains an attribute named ‘id’, in which the value contains ‘search’. It doesn’t need to be exactly the ‘search’ but the pattern needs to match. Function ‘contains()’ match attribute name with value by matching pattern.
+
* 'search'  값이 포함 된 'id'라는 속성이 포함 된 div 태그를 찾습니다.  
Here is another use of function ‘contains()’:
+
* 정확히 'search'일 필요는 없지만 패턴이 일치해야합니다.  
 
+
* 함수‘contains ()’는 패턴을 일치시켜 속성 이름을 값과 일치시킵니다.
 +
# 다음은‘contains ()’함수의 또 다른 사용법
 +
<source lang=xml>
 
.//span[contains(text(), 'Hot')]
 
.//span[contains(text(), 'Hot')]
 +
</source>
 +
* 이 표현식은 텍스트로 'Hot'이 포함 된 span 태그를 찾습니다.
 +
* 키워드 "text ()"를 사용한 표현식 :
 +
* 앞에서 본 예에서는‘text ()’함수를 사용했습니다.
 +
# 'text ()’함수의 특정 텍스트가있는 특정 태그를 찾습니다.
 +
<source lang=xml>
 +
.//span[text()= 'Home']
 +
</source>
  
This expression finds a span tag which contains ‘Hot’ as text in it.
 
Expression using keyword “text()”:
 
In previous example you saw, I used function ‘text()’. Let’s have a deeper look into function ‘text()’. It will find out a specific tag which has specific text in it. Here is an example:
 
  
 
.//span[text()= 'Home']
 
.//span[text()= 'Home']

2021년 5월 27일 (목) 11:50 판

thumb_up 추천메뉴 바로가기


1 Xpath 개요[편집]

Selenium WebDriver를 사용하여 웹 애플리케이션을 자동화 할 때 가장 어려운 작업 중 하나는 웹 요소를 찾는 것입니다.

Selenium WebDriver는 다양한 로케이터 전략을 지원합니다.

  1. Id
  2. CSS
  3. XPATH
  4. Class Name
  5. Name
  6. Tag Name
  7. Links Text
  8. Partial Links Text

Among all the locator strategies Id is the best locator, as a standard, Id is unique in a web page. But in real life nothing is perfect. In real life scenario you will hardly get Id in every element. Here comes the need for the rest of the locator strategies.

Keeping this in mind I am going to write a series of blogs regarding locator strategies. In this blog I will try to focus on XPath Basics.

1.1 XPath[편집]

  1. XML 경로 언어 인 XPath는 XML 문서에서 노드를 선택하기위한 쿼리 언어.
  2. XPath 언어는 XML 문서의 트리 표현 (계층 적)을 기반으로하며 다양한 기준에 따라 노드를 선택하여 트리를 탐색 할 수있는 기능을 제공.
  3. XPath로 요소를 찾는 것은 많은 유연성으로 매우 잘 작동합니다.
  4. 느린 성능으로 인해 가장 선호되지 않는 로케이터 전략.
  5. XPath와 CSS의 중요한 차이점 중 하나는 XPath를 사용하여 DOM 계층 구조에서 요소를 앞뒤로 검색 할 수 있지만 CSS는 순방향으로 만 작동한다는 것입니다.
    XPath를 사용하여 자식 요소를 사용하여 부모 요소를 찾을 수 있음을 의미합니다.
  6. XPath는 대소 문자를 구분하는 언어입니다.
  7. HTML 페이지는 DOM에서 XHTML 문서로 표시되고 요소를 찾기 위해 Selenium WebDriver에서 사용되므로 모든 주요 브라우저는 XPath를 지원.
  8. XPath를 찾는 데 사용할 수있는 여러 도구가 있습니다. Firefox 브라우저 Firebug 및 XPath Checker 추가 기능 및 Chrome XPath Helper 및 Toggle XPather의 경우.

1.2 XPath 표현식[편집]

XPath expression generally defines a pattern in order to select a set of nodes. XPath uses path expression to select nodes or list of nodes from an XML/html document. This path can be of two type:

1.2.1 절대경로 (Absolute Path)[편집]

  1. 절대 경로는 계층 문서 내에서 노드를 선택하기 위해 모든 단일 노드를 지정하는 것을 의미합니다.
  2. 항상 루트 노드에서 시작합니다.
  3. 예시
/html/body/div[2]/div/div[4]/div/ul/li[3]/a/span

1.2.2 상대경로 (Relative Path)[편집]

  1. 상XPath 표현식은 매우 길 수 있습니다.
  2. 중첩 된 각 관계에 대해 100% 존재해야 함. 그렇지 않으면 로케이터가 작동하지 않습니다.
    이러한 이유로 Relative XPath 표현식을 사용하는 것이 좋습니다.
  3. 상대 XPath 표현식에서는 선택한 노드에서 시작할 수 있습니다.
  4. 위의 절대 경로를 다음 상대 경로에도 쓸 수 있습니다.
//div[@id='navigation']/ul/li[3]/a/span

1.2.3 슬래쉬 “/” 와 "//" 의미[편집]

  1. XPath의 어느 곳에서나 하나의 슬래시 '/'는 상위 요소 내에서 바로 요소를 찾는 것을 의미합니다.
  2. 이중 슬래시‘//’는 상위 요소 내에서 하위 또는 손자 (하위) 요소를 찾는 것을 나타냅니다.
  3. 두가지 예시 참조
  4. 예시 1)
.//div[@id='products']/a/img
    1. 'a' 태그의 하위 인 html 문서에서 이미지 태그를 찾으려고합니다.
    2. 그리고 a 태그는 div 태그의 자식입니다.
    3. 동일한 img 태그는 다음에서 찾을 수 있습니다.
.//div[@id='products']//img

1.2.4 점 “.” 와 “..” 의미[편집]

  1. "." 은 XPath에서 현재 노드를 지정하는 데 사용됩니다.
  2. ".." 은 문서 트리에서 한 단계 위로 이동하는 데 사용됩니다.
    즉, 현재 노드의 부모를 찾습니다.
  3. 예시)
.//div[@id='products']/..
  • 위 표현식은 현재 노드의 부모 노드를 찾습니다.


1.2.5 "@”의 의미[편집]

  1. "@"는 문서 트리에서 속성을 선택하는 데 사용됩니다.
  2. 위의 예시에서 "@"로 속성 ID를 지정하고‘’안에 값 'products' 지정 함.

1.3 = “*”의 의미[편집]

  1. XPath는 "*"와 같은 와일드 카드를 지원합니다.
    때로는 상대 경로를 찾는 데 많이 유용합니다.
  2. 예시 )
.//*[text()='Home']
.//a[@*='Home']
  • 이 경우 값이 'home'인 속성을 가진 요소를 찾으려고합니다.
  • 이 경우 속성 이름은 신경 쓰지 않습니다.

1.3.1 XPath 편리 팁 사용법[편집]

  1. 대부분의 경우 로케이터의 속성이 동적으로 생성 될 때 문제가 발생합니다.
    그리고 지금은 거의 모든 웹 애플리케이션이이 전략을 따르고 있습니다.

1.3.2 = “contains” 키워드 사용하는 표현식[편집]

  1. "contains ()"는 속성이름과 속성 값 , 두 개의 인수를받는 함수입니다.
.//div[contains(@id, 'search')]
  • 'search' 값이 포함 된 'id'라는 속성이 포함 된 div 태그를 찾습니다.
  • 정확히 'search'일 필요는 없지만 패턴이 일치해야합니다.
  • 함수‘contains ()’는 패턴을 일치시켜 속성 이름을 값과 일치시킵니다.
  1. 다음은‘contains ()’함수의 또 다른 사용법
.//span[contains(text(), 'Hot')]
  • 이 표현식은 텍스트로 'Hot'이 포함 된 span 태그를 찾습니다.
  • 키워드 "text ()"를 사용한 표현식 :
  • 앞에서 본 예에서는‘text ()’함수를 사용했습니다.
  1. 'text ()’함수의 특정 텍스트가있는 특정 태그를 찾습니다.
.//span[text()= 'Home']


.//span[text()= 'Home']

As I said this expression will find a span tag with text ‘Home’.

Expression using keyword “starts-with()”: Like function ‘contains()’, ‘starts-with()’ also take two arguments, attribute name and attribute value. Let’s look it in an example:

.//div[starts-with(@class, 'width')]

Here I am trying to find a div tag where there is an attribute ‘class’ that starts with pattern ‘width’. This function also find element by matching pattern. Another example:

.//span[starts-with(text(), 'Gift')]

As you saw earlier in ‘contains()’ function I used ‘text()’ function to find text inside that tag. That can be also done with function ‘starts-with()’ .

Conclusion: In this blog, I focused on a number of the tricks and techniques that you will use daily as a Test Automation Engineer. There is no end of learning. You can learn more about XPath. Next time I will try to focus on some advance topic in XPath. Till then Practice…