Home > other >  Unable to access the button in Selenium Python using Xpath
Unable to access the button in Selenium Python using Xpath

Time:04-26

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 >To reset your password, enter your Salesforce username.</p>
            <input type="hidden" name="locale" value="in" />
            
            <div >
                <label for="un" >Username</label>
                <input id="un" type="email"  onKeyPress="checkCaps(event)"
                       name="un" value="" autofocus />
                
                <input type="submit" name="continue" id="continue"  value="Continue" />
                <input type="button" name="cancel" onclick="parent.location='/?locale=in'"   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  href="javascript:document.forgotPassword.submit();" id="continueReset">Continue</a>
            
                <p >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()

CodePudding user response:

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

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

Solution

To click element instead of enter image description here

CodePudding user response:

The element you are trying to access is input element NOT button element.

Use following xpath to identify the element.

driver.find_element(By.XPATH, "//input[@name='cancel' and @value='Cancel']").click()

To handle dynamic element use WebDriverWait() and wait for element to be clickable.

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='cancel' and @value='Cancel']"))).click()

You need to import below libraries.

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