4

I have an "onclick" element in a webpage,

<a href="javascript:void(0)" onclick=
    "toIndexHtml('3','http://xxxxxxxxxxx/trade','0')">
<i></i>
<span></span>
trade
</a>

It is shown in the webpage as a button and I want to click on it, I tried to search for it using the following code:

driver.find_element_by_xpath("//a[contains(@onclick,'toIndexHtml')]").click()
WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.XPATH,"/html/body/div/ul/li[3]/a"))).click()

Both are not working, please suggest if there is any other ways! Thanks in advance!

P.S.: I am using Chrome WebDriver and Chrome v64.

1 Answer 1

1

Your first locator looks perfect and should have worked.

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='toIndexHtml']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick,'toIndexHtml')]"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! just figured out my element is in an iframe that I need to switch to that frame before locating it:)
@lambda COuld you explain a bit more? I have the same problem, and the answer above won't work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.