복붙노트

[PYTHON] python selenium 버튼을 클릭하십시오.

PYTHON

python selenium 버튼을 클릭하십시오.

나는 Python 셀렌에 아주 익숙하고 다음 html 구조를 가진 버튼을 클릭하려고한다.

<div class="b_div">

    <div class="button c_button s_button" onclick="submitForm('mTF')">
        <input class="very_small" type="button"></input>
        <div class="s_image"></div>
        <span>
           Search
        </span>
    </div>

    <div class="button c_button s_button" onclick="submitForm('rMTF')" style="margin-bottom: 30px;">
        <input class="v_small" type="button"></input>
        <span>
              Reset
        </span>
   </div>

</div>

위의 검색 및 재설정 버튼을 모두 클릭 할 수 있기를 원합니다 (분명히 개별적으로).

나는 두 가지를 시도했다. 예를 들면 :

driver.find_element_by_css_selector('.button .c_button .s_button').click()

또는,

driver.find_element_by_name('s_image').click()

또는,

driver.find_element_by_class_name('s_image').click()

하지만, 나는 항상 NoSuchElementException으로 끝나는 것처럼 보인다. 예를 들면 다음과 같다.

selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"s_image"}' ;

어떻게 든 셀 클릭을 만들기 위해 HTML의 onclick 속성을 사용할 수 있는지 궁금합니다.

올바른 방향으로 나를 가리킬 수있는 모든 생각은 훌륭합니다. 감사.

해결법

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

    1.CSS 선택기에서 클래스 사이의 공백을 제거하십시오.

    CSS 선택기에서 클래스 사이의 공백을 제거하십시오.

    driver.find_element_by_css_selector('.button .c_button .s_button').click()
    #                                           ^         ^
    

    =>

    driver.find_element_by_css_selector('.button.c_button.s_button').click()
    
  2. ==============================

    2.이 시도:

    이 시도:

    firefox를 다운로드하고, "firebug"및 "firepath"플러그인을 추가하십시오. 그들을 설치 한 후 귀하의 웹 페이지로 이동, 방화범이 끌려 시작하고 요소의 xpath를 찾으십시오, 그것은 페이지에서 고유하므로 실수 할 수 없습니다.

    그림보기 :

    browser.find_element_by_xpath ( 'Xpath를 복사하여 붙여 넣기').

  3. ==============================

    3.Phantomjs를 브라우저로 사용하는 것과 같은 문제가있어서 다음과 같이 해결했습니다.

    Phantomjs를 브라우저로 사용하는 것과 같은 문제가있어서 다음과 같이 해결했습니다.

    driver.find_element_by_css_selector('div.button.c_button.s_button').click()
    

    본질적으로 견적에 DIV 태그의 이름을 추가했습니다.

  4. ==============================

    4.다음 디버깅 프로세스를 통해 비슷한 문제를 해결할 수있었습니다.

    다음 디버깅 프로세스를 통해 비슷한 문제를 해결할 수있었습니다.

    with open("output_init.txt", "w") as text_file:
        text_file.write(driver.page_source.encode('ascii','ignore'))
    
    
    xpath1 = "the xpath of the link you want to click on"
    destination_page_link = driver.find_element_by_xpath(xpath1)
    destination_page_link.click()
    
    
    with open("output_dest.txt", "w") as text_file:
        text_file.write(driver.page_source.encode('ascii','ignore'))
    

    그런 다음 초기 페이지 ( 'output_init.txt')와 버튼을 클릭 한 후 전달 된 페이지 ( 'output_dest.txt')가있는 두 개의 텍스트 파일을 가져야합니다. 동일하다면, 코드가 작동하지 않습니다. 그렇지 않은 경우 코드가 작동하지만 다른 문제가 있습니다. 저를위한 문제점은 나의 걸이를 만들기 위하여 내용을 변형시킨 필요한 javascript가 아직 실행되지 않았다는 것을 보였다.

    내가보기에 당신의 선택 :

    xpath 접근 방식이 반드시 더 좋은 것은 아니며 단지 선호합니다. 선택기 방식을 사용할 수도 있습니다.

  5. from https://stackoverflow.com/questions/21350605/python-selenium-click-on-button by cc-by-sa and MIT license