2

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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Tac
  • 23
  • 3
  • I cannot see your image. Can you please update your question to reflect the community guidelines and make it easier for me to help you – DeltaRage Apr 25 '22 at 10:15

3 Answers3

1

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried this clearly there's no more error but I am still unable to see the transitions between various steps. I understand this is working but for instance, my username field should have my input 'Tac' entered in the field>> then password>> clear the password>>clicking of Forgot my Password>> transition to the recover password page>>clicking of cancelling button and coming back to the login page but it is just landing on the login page and highlighting the username field and Pycharm console and no error. – Tac Apr 25 '22 at 15:11
  • Also, can you please explain why exactly we added WebDriverWait and 20 etc. – Tac Apr 25 '22 at 15:11
  • @Tac If you visit the embedded link each and every aspect used in this answer is described in details as an example [What does WebDriverWait(driver, 20) means?](https://stackoverflow.com/a/71951054/7429447) – undetected Selenium Apr 25 '22 at 15:26
0

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

DeltaRage
  • 119
  • 1
  • 3
  • 16
0

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
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • So, I am learning through a Udemy course and he specified this way as a way apart from the way you have mentioned. Also, in his case , the course is few months old so the css html have few changes as in instead of input tag there was an anchor tag so his code worked for him with this syntax and he didn't added WebDriverWait statement as well. Can you please help me why he was able to see all the transitions? – Tac Apr 25 '22 at 15:19