2013-02-24 3 views
4

파이썬 셀렌 코드로 텍스트 상자에 쓰려고하는데 텍스트 상자의 부모 태그가 숨겨져 있기 때문에 오류가 발생합니다.셀레늄 파이썬 스크립트로 숨겨진 요소에 값 쓰기

driver.find_element_by_xpath("//input[@itemcode='XYZ']").send_keys(1) 

자바로 인해 실행 파일을 자바 스크립트로 해결할 수 있지만 파이썬 스크립트와 비슷한 기능이 필요합니다.

미리 감사드립니다.

답변

5

이 해결 방법 (파이어 폭스와 크롬에서 테스트)를보십시오 : (그렇지 않으면를 검색 할 수있는 JS 코드를 사용, 텍스트 상자의 ID가 "XYZ"입니다 가정)

from selenium import webdriver 
from selenium.common.exceptions import NoSuchElementException 

browser = webdriver.Firefox() # Get local session(use webdriver.Chrome() for chrome) 
browser.get("http://www.example.com") # load page from some url 
assert "example" in browser.title # assume example.com has string "example" in title 

try: 
    # temporarily make parent(assuming its id is parent_id) visible 
    browser.execute_script("document.getElementById('parent_id').style.display='block'") 
    # now the following code won't raise ElementNotVisibleException any more 
    browser.find_element_by_xpath("//input[@itemcode='XYZ']").send_keys(1) 
    # hide the parent again 
    browser.execute_script("document.getElementById('parent_id').style.display='none'") 
except NoSuchElementException: 
    assert 0, "can't find input with XYZ itemcode" 

가 다른 해결 방법도 간단하고 아마 텍스트 상자의 값만 변경하려면 다음을 수행하십시오.

browser.execute_script("document.getElementById('XYZ').value+='1'") 
+0

감사합니다. 지금은 매력처럼 작동합니다. – vikram

+0

요소에 계속 변경되는 ID가 있으면 어떻게됩니까? 클래스 이름 또는 CSS 선택자로 찾은 다음이를 보이게 할 수 있습니까? –

관련 문제