0

This seems pretty straightforward. I am trying click on a submit button via selenium chrome web driver which obviously quite straight forward. But for some reason, I am not able to do so.

here is how the code look like:-

<div class="prettyBtn primaryBtn" onclick="setIsDataAdjustHiddenField();QuoteFormSubmit();return false;"> <span> Get Quote </span></div>

attaching image of code snippet.

enter image description here

so far, I have tried xpath, full Xpath of class "btn", class "prettyBtn primaryBtn".

after no success, I thought of spying them with the class name "find_elements_by_class_name" and still not able to click the button.

here is what I have tried so far:-

via Span Tag:

driver.find_elements_by_xpath("//span[text()='Get Quote']").click()

via class name:

driver.find_elements_by_class_name("btn").click() 
driver.find_elements_by_class_name("prettyBtn primaryBtn").click()

I did look into the documentation and couple of other soultion but so far no luck...I have other web pages where I was able to fill and submit the form but this one. any help or suggestions ?

1 Answer 1

3

The innerText within the <span> contains leading and trailing whitespaces around it which you have to consider and you can use either of the following Locator Strategies:

  • Using xpath and normalize-space():

    element = driver.find_element(By.XPATH, "//span[normalize-space()='Get Quote']")
    
  • Using xpath and contains():

    element = driver.find_element(By.XPATH, "//span[contains(., 'Get Quote')]")
    

To click on the element ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using xpath and normalize-space():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[normalize-space()='Get Quote']"))).click()
    
  • Using xpath and contains():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Get Quote')]"))).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.

1 Comment

thanks.....completely missed the extra spaces. and what a nice user name @udentected selenium

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.