Search code examples
pythonseleniumselenium-webdriverxpathwebdriverwait

Unable to access the button in Selenium Python using Xpath


Error Msg:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[text()="Cancel"]"}

I have tried with tagname as button too.

The HTML for this:

<form name="forgotPassword" id="forgotPassForm" action="https://login.salesforce.com/secur/forgotpassword.jsp"
              autocomplete="off" method="post" target="_top" novalidate="novalidate">
            <p class="username">To reset your password, enter your Salesforce username.</p>
            <input type="hidden" name="locale" value="in" />
            
            <div class="verifyform">
                <label for="un" class="label">Username</label>
                <input id="un" type="email" class="input wide mb12 mt8 username" onKeyPress="checkCaps(event)"
                       name="un" value="" autofocus />
                
                <input type="submit" name="continue" id="continue" class="button primary fiftyfifty right focus" value="Continue" />
                <input type="button" name="cancel" onclick="parent.location='/?locale=in'" class="secondary button fiftyfifty mb16"  value="Cancel" >
            </div>
            
                <input type="hidden" name="lqs" value="locale=in" />
            
            <div id='pwcaps' class='pwcaps' style='display:none'>Caps Lock is on. Please try to log in again and remember that passwords are case-sensitive.</div>
            <a class="hiddenlink" href="javascript:document.forgotPassword.submit();" id="continueReset">Continue</a>
            
                <p class="small">Video: <a href="http://salesforce.vidyard.com/watch/MxeeKTO3x5oMx4jNVWWX4w" id="video-link">Need Help Logging In?</a></p>
            
        </form>

My code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(service=Service("C:\\Users\\Dell\\Desktop\\Selenium\\chromedriver.exe"))
driver.maximize_window()
driver.get("https://login.salesforce.com/")
driver.find_element(By.CSS_SELECTOR, '#username').send_keys('Tac')
driver.find_element(By.CSS_SELECTOR, '.password').send_keys('PASSWRd')
driver.find_element(By.CSS_SELECTOR, '.password').clear()
driver.find_element(By.LINK_TEXT, 'Forgot Your Password?').click()
driver.find_element(By.XPATH, '//button[text()="Cancel"]').click()

Solution

  • The element Cancel doesn't have a innerText rather the value attribute is set as Cancel

    <input type="button" name="cancel" onclick="parent.location='/'" class="secondary button fiftyfifty mb16" value="Cancel">
    

    Solution

    To click element instead of presence_of_element_located() 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, "input[value='Cancel']"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Cancel']"))).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