Selenium에서 요소의 속성을 얻는 방법은 무엇입니까?


90

저는 Python에서 Selenium으로 작업하고 있습니다. 나는를 좀하고 싶습니다 .val()(A)의 <select>요소와 그것이 내가 기대하는 것입니다 있는지 확인합니다.

이것은 내 코드입니다.

def test_chart_renders_from_url(self):
    url = 'http://localhost:8000/analyse/'
    self.browser.get(url)
    org = driver.find_element_by_id('org')
    # Find the value of org?

어떻게 할 수 있습니까? Selenium 문서는 요소 선택에 대해서는 많지만 속성에 대해서는 아무것도없는 것 같습니다.


2
selenium-python-docs, 7.11 get_attribute(name) 은 작업을 수행 할 수 있지만 실제로 사용하지는 않았습니다. 한번 시도해보세요!
— Abd Azrad

답변:


138

당신은 아마도 get_attribute(). 여기에 예가 나와 있습니다. 뿐만

def test_chart_renders_from_url(self):
    url = 'http://localhost:8000/analyse/'
    self.browser.get(url)
    org = driver.find_element_by_id('org')
    # Find the value of org?
    val = org.get_attribute("attribute name")

49

파이썬

element.get_attribute("attribute name")

자바

element.getAttribute("attribute name")

루비

element.attribute("attribute name")

씨#

element.GetAttribute("attribute name");

6

최근 개발 된 웹 애플리케이션 이 JavaScript , jQuery , AngularJS , ReactJS 등을 사용하기 때문에 Selenium 을 통해 요소의 속성을 검색 하려면 WebDriver 인스턴스를 이전 의 웹 브라우저 와 같은 지연된 웹 클라이언트 와 동기화 하도록 WebDriverWait 를 유도해야 할 가능성이 있습니다. 속성을 검색하려고합니다.

몇 가지 예 :

  • 파이썬 :

    • 모든 속성의 형태에게 검색하려면 표시 (예 : 요소를 <h1>사용자가 사용할 필요가 태그) expected_conditions을 로 visibility_of_element_located(locator)다음과 같이 :

      attribute_value = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "org"))).get_attribute("attribute_name")
      
    • 모든 속성의 형태에게 검색하려면 상호 작용 (예 : 요소를 <input>사용자가 사용할 필요가 태그) expected_conditions을 로 element_to_be_clickable(locator)다음과 같이 :

      attribute_value = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "org"))).get_attribute("attribute_name")
      

HTML 속성

다음은 HTML에서 자주 사용되는 일부 속성 목록입니다.

HTML 속성

참고 : 각 HTML 요소에 대한 모든 속성의 전체 목록은 다음 위치에 있습니다. HTML 속성 참조

당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.