Home > Blockchain >  Unable to access the button in Selenium Python using Xpath by the command <driver.find_element(By
Unable to access the button in Selenium Python using Xpath by the command <driver.find_element(By

Time:04-25

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:

Have you tried the following code:

driver.find_element(By.XPATH, '//*[@id="forgotPassForm"]/div[1]/input[3]').click()

To get the basic XPath you can inspect element by right clicking on the web page and selecting 'Elements'. You can then right click the button and then 'Copy > Copy XPath'. This will allow you to get the very basics working before then refactoring and using a more robust piece of xcode.

See screenshot:

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