Search code examples
pythonseleniumonclickclick

(Python + selenium) how to click on "onclick" element?


<a href="#none" onclick="MemberAction.snsLogin('googleplus', '%2Findex.html')">

<img src="http://img.echosting.cafe24.com/skin/base_ko_KR/member/btn_google_login.gif" alt="구글 로그인"></a>

I wanted to click on that element, so I tried a few things.

however it said an NoSuchElementExeption error

What else can I try?

driver.find_element_by_xpath('//*[@id="member_form_8077679061"]/div/div[2]/ul/li[3]/a').click()
driver.find_element_by_css_selector('#member_form_3934077016 > div > div.ifwc-easy > ul > li:nth-child(3) > a').click()

Solution

  • The element you are trying to click on could not be there when the webdriver searches for it on the page, which is one potential explanation. This may occur if the element is added dynamically to the page after it has loaded or if the page is still loading when the webdriver attempts to locate the element. In this situation, you may consider pausing the script's execution with an explicit wait until the element appears on the page before attempting to click on it.

    You can try something like this:

    
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    locator = (By.XPATH, 'my-element')
    
    wait = WebDriverWait(driver, 10)
    
    element = wait.until(EC.presence_of_element_located(locator))