from RPA.Browser import Browser br = Browser() br.open_chrome_browser("https://www.google.fi",headless=True) br.input_text("//input[@name='q']", "robocorp") br.screenshot(page=True, locator="//input[@name='q']")
input_text_when_element_is_visible
(locator: str, text: str) → NoneInput text into locator after it has become visible.
locator
element locator
text
insert text to locator
XPath
XPath is a language for traversing the structure of the DOM (document object model) of the web page.
“raw” XPath
To find the link in this page:
<html><body> <p>The fox jumped over the lazy brown <a href="dogs.html">dog</a>.</p> </body></html>
/html/body/p/a
Child of Element ID
XPath can find an element by ID like this:
eliment 의 ID에 의해서도 만들수 있습니다.
//*[@id="element_id"]
So if you need to find an element that is near another element with an ID, like the link in this example:
<html><body> <p id="fox">The fox jumped over the lazy brown <a href="dogs.html">dog</a>.</p> </body></html>
you could try an XPath like this to find the first link that is a child of the element with ID=”fox”:
//*[@id="fox"]/a
위 예제에서는 fox id까지 id를 판단하고 설정하고 나서 /a raw XPath를 사용하였습니다.
Button Text
There are two ways to declare a standard button in HTML, discounting the many ways to make something that looks like a button, but is not. To determine how an element is declared in the HTML, see how to inspect an element in the browser.
If the button is declared with the <button> tag and the button says “press me”, try this:
//button(contains(., 'press me')]
If the button is a form submit button (declared with the <input> tag and type=”submit” or =”button”) and says “press me”, try this:
//input[@value='press me']
버튼의 text를 가지고 위와 같이 설정할 수 있습니다.
Text of element
Sometimes a bit of text is styled as a link or button. To find it, try this:
//*[text()='the visible text']
이건 텍스트를 나타낼때
The Nth element
To find the Nth element, you must surround your XPath in ()s and then specify the Nth using [n], like this:
(xpath)[n]
A very simple example – find the 3rd link on a page:
(//a)[3]
To find the 4rd text input field on a page:
(//input[@type="text"])[4]
You can also traverse from the indexed element. So, to find the link in the 2nd div with class ‘abc’:
(//div[@class='abc'])[2]/a
마지막으로 몇번째인지 나타낼때 위와 같이 표현하게 됩니다.
결론
그런데 매번 html을 분석해서 지정하기는 힘듭니다. 다행히 크롬 브라우저에 해당 기능이 있어서 쉽게 Xpath를 사용할 수가 있습니다.
브라우저의 F12 키를 눌러 Eliment를 열고 적당한 위치를 찾았으면 마우스 오른쪽 버튼 눌러 Copy를 선택합니다.
www.google.com의 입력하는 위치의 XPath와 full XPath 입니다.
XPath : //*[@id="fakebox-input"]
full XPath(raw XPath) : /html/body/div[4]/div[2]/div/input
테스트
1.html
<html> <body> Text <form> <input type='text' id='q' name='qq'></input> </form> </body> </html>
Test Code:
from RPA.Browser import Browser br = Browser() br.open_chrome_browser("file:///C:/Users/USER/Documents/python/rpa/1.html") br.input_text('//*[@id="q"]', "robocorp") br.screenshot(page=True) # br.input_text("/html/body/form/input", "robocorp") <<= 에러발생 # br.screenshot(page=True)
댓글 없음:
댓글 쓰기