복붙노트

[PYTHON] Selenium Compound 클래스 이름은 허용되지 않습니다.

PYTHON

Selenium Compound 클래스 이름은 허용되지 않습니다.

아래 코드를 사용하여 요소를 클릭하여 화면을 팝업하고 텍스트를 복사합니다.

el1 = driver.find_element_by_id("keyDev-A")
el1.click()
el2 = driver.find_element_by_class_name("content")
print(el2.text)

그러나 셀레늄을 사용하여 해당 팝업 내의 버튼을 클릭하면

el3 = driver.find_element(By.CLASS_NAME, "action-btn cancel alert-display")
el3.click()

오류 메시지가 나타납니다. 유효하지 않은 선택자 : 복합 클래스 이름이 허용되지 않습니다.

이것은 셀렌이 클릭하려고하는 HTML입니다. 닫기 버튼.

<div class="nav">
    <span class="action-btn confirm prompt-display">Confirm</span>
    <span class="action-btn cancel prompt-display">Cancel</span>
    <span class="action-btn cancel alert-display">Close</span>
</div>

닫기 버튼을 클릭하려면 어떻게 작성해야합니까?

해결법

  1. ==============================

    1.Leon의 코멘트는 복합 클래스 이름이 더 이상 지원되지 않는다는 정확한 정보를 제공합니다. 대신 CSS 선택기를 사용해보십시오. 귀하의 경우 다음 코드 줄을 사용하여 원하는 요소를 얻을 수 있습니다.

    Leon의 코멘트는 복합 클래스 이름이 더 이상 지원되지 않는다는 정확한 정보를 제공합니다. 대신 CSS 선택기를 사용해보십시오. 귀하의 경우 다음 코드 줄을 사용하여 원하는 요소를 얻을 수 있습니다.

    el3 = driver.find_element_by_css_selector(".action-btn.cancel.alert-display")
    

    클래스 속성에서 세 가지 클래스 (action-btn, cancel 및 alert-display)가 모두있는 요소를 찾습니다. 여기서 클래스의 순서는 중요하지 않으며 클래스 중 하나라도 클래스 속성의 아무 곳에 나 나타날 수 있습니다. 요소에 세 개의 클래스가 모두있는 경우 요소가 선택됩니다. 클래스의 순서를 고정하려면 다음 xpath를 사용할 수 있습니다.

    el3 = driver.find_element_by_xpath("//*[@class='action-btn cancel alert-display']") 
    
  2. ==============================

    2.나는이 질문에 늦었다. 그러나 Xpath에 익숙하지 않은 경우에는 compound 클래스를 String으로 처리하고 tag_name을 사용하고 get_attribute ( 'class')를 사용하여 해결 방법을 찾았습니다. 몇 줄의 코드가 필요하지만 나처럼 직선적이며 초보자에게 적합합니다.

    나는이 질문에 늦었다. 그러나 Xpath에 익숙하지 않은 경우에는 compound 클래스를 String으로 처리하고 tag_name을 사용하고 get_attribute ( 'class')를 사용하여 해결 방법을 찾았습니다. 몇 줄의 코드가 필요하지만 나처럼 직선적이며 초보자에게 적합합니다.

       elements = driver.find_elements_by_tag_name('Tag Name Here')
            for element in elments:
                className = watchingTable.get_attribute('class')
                print(className)
                    if className == 'Your Needed Classname':
                        #Do your things
    
  3. from https://stackoverflow.com/questions/37771604/selenium-compound-class-names-not-permitted by cc-by-sa and MIT license