์๋ฐ์คํฌ๋ฆฝํธ - XPath ๋ค๋ฃจ๊ธฐ, ๋ฐ์์๋ ํ ์คํธ ์ฌ์ดํธ ํต ๋ง๋ค๊ธฐ
XPath ์ ํ์์ธ $x๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์์ง๋ง, setInterval ํจ์ ๋ด์ $x ์ ํ์๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ๋ฉด
VM3146:2 Uncaught ReferenceError: $x is not defined
at <anonymous>:2:12
์ด๋ผ๋ ์ค๋ฅ ๋ฉ์์ง๊ฐ ๋์จ๋ค.
๊ทธ๋์ evaluate ๋ฉ์๋๋ฅผ ์ด์ฉํด XPath ํํ์์ผ๋ก ํน์ ์์๋ฅผ ์ฐพ๋ ๋ฐฉ๋ฒ์ ๋์ ํ ์ ์๋ค.
evaluate ํจ์ ๊ตฌ๋ฌธ
evaluate(xpathExpression, contextNode, namespaceResolver, resultType, result)
๋งค๊ฐ ๋ณ์
xpathExpression - XPath ํํ์์ด ์จ๋ค.
contextNode - document ๊ฐ์ context ๋ ธ๋๊ฐ ์ ๋ ฅ๋๋ค.
resultType - ๊ฒฐ๊ณผ ์ ํ์ ๋ง๋ XPathResult๋ฅผ ๋ฐํ
๋ฐ์์๋ ํ ์คํธ ์ฌ์ดํธ ํต ๋ง๋ค๊ธฐ
evaluate ๋ฉ์๋๋ฅผ ์ด์ฉํด ๋ฐ์์๋ ํ ์คํธ ์ฌ์ดํธ ํต์ ๋ง๋ค์๋ค. setInterval ํจ์๋ก 0.001์ด(1ms)๋ง๋ค "ํด๋ฆญํด์ฃผ์ธ์"๋ผ๋ ์์๊ฐ ์๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํด ๋ง์ฝ ์๋ค๋ฉด .reaction-click์ ํด๋ฆญํ๋ค.
setInterval(()=>{
if(document.evaluate('//*[text()="ํด๋ฆญํด์ฃผ์ธ์"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue)
document.querySelector('.reaction-click').click()
}, 1 )
์ฐธ๊ณ :
https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate
Document: evaluate() method - Web APIs | MDN
The evaluate() method of the Document interface selects elements based on the XPath expression given in parameters.
developer.mozilla.org
https://wonyoung2.tistory.com/843
[Javascript] XPath ์ด์ฉํ๊ธฐ
Javascript์์ XPath๋ฅผ ์ด์ฉ ํน์ Element๋ฅผ ์ฐพ์๋ ์๋์ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค. var xpath = "//a" var element = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; ## XPathResult - ANY_TY
wonyoung2.tistory.com