This is the page I am trying to scrape: https://directory.brcgs.com/site/10005068
My code:
bk_btn = driver.find_element_by_xpath("//button [@class='BackButton_backButton__3Czsm']")
bk_btn.click()
This is the error I receive:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button [@class='BackButton_backButton__3Czsm']"}
Why am I getting this error and how can I correct it?
CodePudding user response:
Your locator is correct.
It's quite clear that you are missing a delay.
You need to make the element completely loaded before clicking it.
The preferred way to do that is to use WebDriverWait
explicit_waits
as following:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//button[@class='BackButton_backButton__3Czsm']"))).click()
CodePudding user response:
Your syntax seems to be outdated. Selenium no longer uses a syntax like driver.find_element_by_xpath
it's now driver.find_element(By.XPATH, 'xpath example')
.