1

Keep getting unable to locate element error message.

The first find element by xpath was fine, but the second one is giving me a hard time.

Here is my code:

import XLUtils
from selenium import webdriver

driver=webdriver.Chrome(executable_path="C:\Chrome_Driver\Chromedriver.exe")
driver.get("https://www.canada.ca/en/revenue-agency/services/e-services/e-services-businesses/payroll-deductions-online-calculator.html")

driver.find_element_by_xpath('/html/body/main/div[1]/div[7]/p/a[1]').click()
driver.find_element_by_xpath('//*[@id="welcome_button_next"]').click()
2
  • You don't want to use XPaths with that many levels or indices because it makes them brittle (more likely to break). A better first locator would be a CSS selector, a.btn-primary. For the second locator, don't find by XPath when you have just an ID. Instead use .find_element_by_id("welcome_button_next"). Commented Apr 22, 2019 at 14:23
  • Also, you need to add the relevant HTML to the question. Providing the link is nice but if the HTML changes in the future, the question won't be useful and can't stand on its own. Commented Apr 22, 2019 at 21:44

2 Answers 2

1

You'll need to wait a bit for the Next button to appear.

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

wait = WebDriverWait(driver, 10)

e = wait.until(
    EC.presence_of_element_located((By.XPATH, '//*[@id="welcome_button_next"]'))
    )
e.click()
Sign up to request clarification or add additional context in comments.

2 Comments

You don't want to wait for presence here and then click. Presence indicates that the element is in the DOM but not necessarily visible or clickable. You instead want to wait for clickable.
Thank you for your comment! Really appreciate it!
0

To click on the element with text as Next you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.btn.btn-primary#welcome_button_next"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='btn btn-primary' and @id="welcome_button_next"]"))).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
    

2 Comments

Instead of "spamming" questions with more answers that are practically exactly the same as existing answers, why don't you help correct other answers that are near perfect?
Thank you for your comment! Really appreciate it!

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.