콘솔워크

[selenium] XPATH로 두 개 이상의 텍스트가 일치하는 web element 찾는 법 본문

프로그래밍/python

[selenium] XPATH로 두 개 이상의 텍스트가 일치하는 web element 찾는 법

이휘재123 2022. 7. 28. 15:51
반응형

한 줄 요약

table 태그에서 tr을 찾아야 할 때 두 가지 이상의 텍스트로 자료를 찾아야 하는 상황이 나왔을 때 만들었다.

order_tr = driver.find_element(By.XPATH, f"//tr[./td[text()='{text1}'] and ./td[text()='{text2}']]")

각종 연산자들도 사용 가능 할 것으로 보임

 

 

특정 텍스트가 포함된 checkbox 찾기.

텍스트는 buyer와 phone 변수로 이루어져 있다.

 

크롬 콘솔창에서 찾는법

$x('//tr[./td[//span[contains(text(), "홍길동")]] and ./td//span[contains(text()[2], "010-1234-1234")]]')

 

셀레니움으로 찾는법

checkbox_for_buyer = driver.find_elements(
    f'//tr[./td[//span[contains(text(), {buyer})]] and ./td//span[contains(text()[2], {phone})]]//input[@type="checkbox"]'
)

 

 

 

참고자료

https://stackoverflow.com/questions/1064968/how-to-use-xpath-contains-here

 

How to use XPath contains() here?

I'm trying to learn XPath. I looked at the other contains() examples around here, but nothing that uses an AND operator. I can't get this to work: //ul[@class='featureList' and contains(li, 'Model...

stackoverflow.com

https://www.javatpoint.com/xpath-operators

 

XPath Operators - javatpoint

XPath Operators for beginners and professionals with examples on absolute path, relative path, syntax, nodes, axes, operators, wildcard, predicate etc.

www.javatpoint.com

 

반응형