2017-12-12 4 views
1

나는 오른쪽 상단 모서리에있는 https://www.pcfinancial.ca/의 드롭 다운 메뉴에서 두 번째 옵션을 선택하는 데 도움이되는 프로그램 작업을하고 있습니다. 여기에 지금까지 내 코드입니다 :Python Selenium을 사용하여 요소를 클릭 할 수 없습니다.

import time 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.support import ui 

driver = webdriver.Chrome() 

driver.get('https://www.pcfinancial.ca/'); 
driver.find_element_by_xpath('//*[@id="lnkSignInOp"]').click() #click on dropdown menu - working 
driver.find_element_by_xpath('//*[@id="PCM"]/a').click() #select "pc mastercard" - not working 

어떤 이상한 것은 내가 처음 driver.find_elements_by_xpath(...) 줄을 사용하여 드롭 다운 메뉴에 액세스 할 수 있습니다,하지만 두 번째 옵션을 선택할 때 나는 다음과 같은 오류가 발생합니다.

Traceback (most recent call last): 
    File "C:\Users\Imad\Documents\Programming\Python\test.py", line 10, in <module> 
    driver.find_element_by_xpath('//*[@id="PCM"]/a').click() #open up dropdown menu works 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click 
    self._execute(Command.CLICK_ELEMENT) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 501, in _execute 
    return self._parent.execute(command, params) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute 
    self.error_handler.check_response(response) 
    File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible 
    (Session info: chrome=63.0.3239.84) 
    (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.16299 x86_64) 

아무도 나에게 무슨 일이 일어나고 있고 어떻게 해결할 수 있는지 이해할 수 있습니까? 미리 감사드립니다!

답변

2

이것은 타이밍 문제의 전형적인 경우입니다. 드롭 다운을 연 후 WebDriverWaitelement_to_be_clickable 예상 조건 클릭으로 메뉴 링크를 기다릴 다음 WebSiteWebElement 당으로

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 


driver = webdriver.Chrome() 
wait = WebDriverWait(driver, 10) 

driver.get('https://www.pcfinancial.ca/') 
driver.find_element_by_xpath('//*[@id="lnkSignInOp"]').click() 


wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#PCM a"))).click() 
+0

감사합니다. 웹 페이지가 실제로로드 된 후 대기 시간이 필요하다는 것을 알지 못했습니다. WebDriverWait에 관해서는, 당신은 10 단위가 무엇인지 알고 있습니까? 그냥 호기심을 불러서 다음번에 "너무 길다"라는 것을 알고 있습니다. –

+1

@ImadKalboneh 문제 없습니다. 다행입니다. 10은 "초"입니다. 감사합니다. – alecxe

0

를 링크 텍스트 PC MasterCard하여 <a> 태그 내에, 다음과 같이 고유 한 xpath을 작성합니다.

+0

응답 해 주셔서 감사합니다.하지만 실제로 시도한 것 중 하나입니다. 슬프게도 나는 같은 오류가있다. 나는 이전 사람으로부터 대답을 얻었다. –

관련 문제