퍼블리셔 커피찬우유

selenium chromedriver xpath 특정 속성을 가진 노드 찾기 본문

파이썬/웹크롤링

selenium chromedriver xpath 특정 속성을 가진 노드 찾기

커피찬우유 2020. 10. 19. 09:54
반응형

selenium chromedriver xpath 특정 속성을 가진 노드

출처 : selenium-python.readthedocs.io/

selenium chromedriver xpath 특정 속성을 가진 노드 찾기

 

셀레니움을 통해 특정 HTML을 찾는 방법을 사용할때 가장 유용한 방법중 하나는

xpath를 사용하는 것입니다.

이걸 사용하면 아주 편하게 원하는 위치를 찾을 수 있습니다.

이것말고도 다른 다양한 방식이 있는데 이것은 추가적으로 공유드리겠습니다.^^

 

python 예제 코드

 

driver.find_elements_by_xpath("//div[@*]")

 

셀레니움

 

  1. 특정 노드 내부
    • / path to / element [@attribute_name]
  2. 문서의 어느 곳에서나
    • // * [@ attribute_name]
  3. 특정 값을 가진 특정 노드 내부
    • / path to / element [@ attribute_name = '검색 값']
    • / path to / element [@ attribute_name = "검색 값"]
  4. 어떤 값을 가진 문서의 어느 곳에서나
    • // * [@ attribute_name = '검색 문자열']
    • // * [@ attribute_name = "검색 문자열"]

 

특정 속성을 가진 노드 찾기

HTML

<Galaxy>

    <name>Milky Way</name>

    <CelestialObject name="Earth" type="planet"/>

    <CelestialObject name="Sun" type="star"/>

</Galaxy>

 

 

XPATH

 

/Galaxy/*[@name]

 

또는

 

//*[@name]

 

찾은 값

 

<CelestialObject name="Earth" type="planet" />

<CelestialObject name="Sun" type="star" />

 

 

불특정 Node 선택

표현 설명
* 매칭 되는 모든 ElementNode
@* 매칭 되는 모든 속성Node
Node() 현재 Node로부터 문서상의 모든 Node를 조회

 

현재 Node와 관련되어 찾는 방법

축명 결과
ancestor 현재 Node의 모든 ancestor( parent, grandparent,..etc)Node들을 선택
ancestor-or-self ancestor + 현재Node
Node() 현재 Node로부터 문서상의 모든 Node를 조회
attribute 현재 Node의 모든 속성Node
child 현재 Node의 모든 자식Node
descendant 현재 Node의 모든 자손Node(child, grandchild..etc)
descendant-or-self descendant + 현재Node
following 현재 Node의 닫기 태그 이후의 문서상 모든 Node
following-sibling 현재 Node 이후의 모든 siblingNode
namespace 현재 Node의 모든 namespaceNode
parent 현재 Node의 부모Node
preceding 현재 Node가 나타나기 이전의 모든 Node (ancestor, namespace, 속성Node 제외)
preceding-sibling 현재 Node 이전의 모든 siblingNode
following-sibling 현재 Node 이후의 모든 siblingNode
self 현재 Node 자기자신

 

xpath 참고 사이트

아래의 참고 사이트에서 더 상세한 내용과 적용 방식을 알 수 있습니다.^^

 

www.w3schools.com/xml/xpath_intro.asp

 

XPath Tutorial

XPath Tutorial What is XPath? XPath is a major element in the XSLT standard. XPath can be used to navigate through elements and attributes in an XML document. XPath stands for XML Path Language XPath uses "path like" syntax to identify and navigate nodes i

www.w3schools.com

www.w3.org/TR/xpath/

 

xpath cover page - W3C

XPath 3.0 (renamed from XPath 2.1 to align with the family of "3.0" specifications) is an expression language that allows the processing of values conforming to the data model defined in [XQuery and XPath Data Model (XDM) 3.0]. Some of the important new fe

www.w3.org

www.w3.org/TR/2017/REC-xpath-31-20170321/

 

XML Path Language (XPath) 3.1

The delimiting terminal symbols are: "!", "!=", StringLiteral, "#", "$", "(", ")", "*", "*:", "+", (comma), "-", (dot), "..", "/", "//", (colon), ":*", "::", ":=", "<", "<<", "<=", "=", "=>", ">", ">=", ">>", "?", "@", BracedURILiteral, "[", "]", "{", "|",

www.w3.org

 

오늘도 즐거운 코딩되세요.

 

감사합니다.

 

--------------------------------------------------------------------------

 

혹시나 도움이 되었다면 아래의 저희 사이트에 오셔서 한번만 구경해주세요.

 

감사합니다.

 

 

스톤모두 유튜브 무료 강의 : https://www.youtube.com/channel/UCZUPZrlC-A8u4Y8KbU0HUvQ?sub_confirmation=1

스톤모두 네이버 스토어 : https://smartstore.naver.com/stonemodoo

스톤모두 모두홈페이지 : https://stone.modoo.at/

스톤모두 워드프레스 홈페이지 : https://stonemodoo.com/

 

반응형
Comments