python3 selenium

python3 selenium XPath를 사용하여 여러 조건으로 요소를 찾는 다양한 방법

Pymac 2023. 11. 23. 14:17
반응형

  XPath를 사용하여 여러 조건으로 요소를 찾는 다양한 방법

 

react-datepicker__time-list-item 클래스를 가지고 텍스트가 "6:00 AM"인 요소를 찾기:

 

<li class="react-datepicker__time-list-item  react-datepicker__time-list-item--selected" tabindex="0">6:00 PM</li>

 

xpath_1 = '//li[contains(@class, "react-datepicker__time-list-item") and contains(text(), "6:00 AM")]'

 

bookPartTime 클래스를 가지고 자식 요소인 button의 텍스트가 "주간"인 요소를 찾기:

 

<ul class="bookPartTime"><li class=""><button>주간</button></li><li class="bookPChoice"><button>야간1</button></li><li class=""><button>야간2</button></li></ul>

 

xpath_2 = '//ul[@class="bookPartTime"]/li/button[text()="주간"]'

 

여러 클래스 포함된 요소 찾기:

<div class="class1 class2">...</div>

xpath_3 = '//*[contains(@class, "class1") and contains(@class, "class2")]'

 

속성과 텍스트로 찾기:

<tag attribute="value">desired_text</tag>

xpath_4 = '//*[@attribute="value" and text()="desired_text"]'

 

부모 요소의 특정 자식 찾기:

<parent_tag> <child_tag>child_text</child_tag> </parent_tag>

xpath_5 = '//parent_tag[child_tag="child_text"]'

 

형제 요소 찾기:

<sibling_tag>sibling_text</sibling_tag>

xpath_6 = '//preceding-sibling::sibling_tag[text()="sibling_text"]'

 

자식 요소 중 n번째 요소 찾기:

<parent_tag> <child_tag position="1">...</child_tag> <child_tag position="2">...</child_tag> <child_tag position="3">...</child_tag> </parent_tag>

xpath_7 = '//parent_tag/child_tag[position()=n]'

 

조건에 따라 여러 요소 찾기:

<div condition1="true">...</div> <div condition2="true">...</div>

xpath_8 = '//*[condition1 or condition2]'

 

XPath 축을 사용하여 조건에 따라 요소 찾기:

<tag attribute="prefix_value_suffix">...</tag>

xpath_9 = '//tag[starts-with(@attribute, "prefix") and contains(@attribute, "substring")]'

 

속성으로 찾기:

<tag attribute="value">...</tag>

xpath_10 = '//*[@attribute="value"]'

 

정확한 텍스트로 찾기:

<tag>exact_text</tag>

xpath_11 = '//tag[text()="exact_text"]'

 

부모 요소로부터 특정 자식 요소 찾기:

<parent_tag> <descendant_tag>...</descendant_tag> </parent_tag>

xpath_12 = '//parent_tag/descendant_tag'

 

특정 속성 값을 가진 요소 찾기:

<tag attribute="desired_value">...</tag>

xpath_13 = '//*[@attribute="desired_value"]'
 

특정 속성 값의 접두사 또는 접미사로 찾기:

<tag attribute="prefix_value">...</tag> <tag attribute="value_suffix">...</tag>

xpath_14 = '//*[@attribute-starts-with="prefix_value"]'
xpath_15 = '//*[@attribute-ends-with="suffix_value"]'

 

특정 속성 값이 포함된 요소 찾기:

<tag attribute="partial_value">...</tag>

xpath_16 = '//*[contains(@attribute, "partial_value")]'

 

특정 속성을 가진 요소 중 n번째 찾기:

<tag attribute="desired_value">...</tag> <tag attribute="desired_value">...</tag>

xpath_17 = '//*[@attribute="desired_value"][position()=n]'

 

XPath 함수를 사용하여 찾기:

<div> <child>...</child> <child>...</child> <child>...</child> </div>

xpath_18 = '//*[count(child::*) > 2]'

 

하위 요소의 개수로 필터링:

<tag> <child>...</child> <child>...</child> <child>...</child> </tag>

 

xpath_19 = '//*[count(*)=3]'

 

특정 부모 요소 아래 모든 자식 요소 찾기:

<parent_tag> <child>...</child> <child>...</child> <child>...</child> </parent_tag>

xpath_20 = '//parent_tag/*'

 

자식 요소 중 마지막 요소 찾기:

<parent_tag> <child>...</child> <child>...</child> <child>...</child> </parent_tag>

xpath_21 = '//parent_tag/*[last()]'

 

앞뒤로 공백이 없는 텍스트를 가진 요소 찾기:

<tag>desired_text</tag>

xpath_22 = '//tag[normalize-space(text())="desired_text"]'

 

현재 요소의 부모 찾기:

<parent_tag> <tag>...</tag> </parent_tag>

xpath_23 = '//tag/..'

 

자식 요소의 특정 위치에 있는 요소 찾기:

<parent_tag> <child_tag>...</child_tag> <child_tag>...</child_tag> <child_tag>...</child_tag> </parent_tag>

xpath_24 = '//parent_tag/child_tag[position()=2]'

 

특정 속성을 가진 요소 중 가장 첫 번째 요소 찾기:

<tag attribute="desired_value">...</tag> <tag attribute="desired_value">...</tag>

xpath_25 = '//*[@attribute="desired_value"][1]'

 

특정 속성을 가진 요소 중 가장 마지막 요소 찾기:

 

<tag attribute="desired_value">...</tag> <tag attribute="desired_value">...</tag>

xpath_26 = '//*[@attribute="desired_value"][last()]'

 

특정 속성 값의 범위에 속하는 요소 찾기:

 

<tag attribute="15">...</tag> <tag attribute="18">...</tag> <tag attribute="25">...</tag>

xpath_27 = '//*[@attribute > 10 and @attribute < 20]'

 

특정 속성 값을 가진 요소 중 텍스트 기준으로 오름차순 정렬된 첫 번째 요소 찾기:

 

<tag attribute="desired_value">1</tag> <tag attribute="desired_value">2</tag> <tag attribute="desired_value">3</tag>

xpath_28 = '//*[@attribute="desired_value" and text()][1]'
반응형