파이썬 셀레늄 클릭 버튼
나는 파이썬 셀레늄을 처음 접했고 다음과 같은 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>
위 의 Search및 Reset버튼을 모두 클릭 할 수 있기를 바랍니다 (분명히 개별적으로).
예를 들어 몇 가지 시도했습니다.
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 속성을 사용하여 셀레늄을 클릭 할 수 있는지 궁금합니다.
나를 올바른 방향으로 인도 할 수있는 어떤 생각이라도 좋을 것입니다. 감사.
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()
이 시도:
firefox를 다운로드하고 "firebug"및 "firepath"플러그인을 추가합니다. 설치 후 웹 페이지로 이동하여 방화범을 시작하고 요소의 xpath를 찾으십시오. 페이지에서 고유하므로 실수하지 마십시오.
browser.find_element_by_xpath('just copy and paste the Xpath').click()
I had the same problem using Phantomjs as browser, so I solved in the following way:
driver.find_element_by_css_selector('div.button.c_button.s_button').click()
Essentially I have added the name of the DIV tag into the quote.
The following debugging process helped me solve a similar issue.
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'))
You should then have two textfiles with the initial page you were on ('output_init.txt') and the page you were forwarded to after clicking the button ('output_dest.txt'). If they're the same, then yup, your code did not work. If they aren't, then your code worked, but you have another issue. The issue for me seemed to be that the necessary javascript that transformed the content to produce my hook was not yet executed.
Your options as I see it:
- 드라이버가 자바 스크립트를 실행하도록 한 다음 찾기 요소 코드를 호출합니다. 이 접근 방식을 따르지 않았으므로 stackoverflow에서 이에 대한 자세한 답변을 찾으십시오.
- 'output_dest.txt'에서 동일한 결과를 생성하는 유사한 후크를 찾으십시오.
- 아무 것도 클릭하기 전에 잠시 기다리십시오.
xpath2 = "클릭 할 xpath"
WebDriverWait (driver, timeout = 5) .until (lambda x : x.find_element_by_xpath (xpath2))
xpath 접근 방식이 반드시 더 나은 것은 아닙니다. 제가 선호하는 것은 선택기 접근 방식을 사용할 수도 있습니다.
참고 URL : https://stackoverflow.com/questions/21350605/python-selenium-click-on-button
'Program Club' 카테고리의 다른 글
| 표 셀에서 텍스트의 수직 정렬 (0) | 2020.11.21 |
|---|---|
| android.content.res.Resources $ NotFoundException : 리소스 ID # 0xffffffff를 찾을 수 없습니다. (0) | 2020.11.21 |
| user.email 및 user.name을 설정하지 않고 커밋 (0) | 2020.11.21 |
| 패닉을 테스트하는 방법? (0) | 2020.11.21 |
| React.createElement : 유형이 잘못되었습니다. 문자열이 필요합니다. (0) | 2020.11.21 |
