I am trying to click a button on a website in Python using Selenium. The html looks like this:
<a class="btn btn-default btn-lg primary-light-blue-btn" onclick="createReport()">Create report</a>
I have tried using:
l = driver.find_element(By.XPATH, "//button[@class='btn btn-default btn-lg primary-light-blue-btn']")
But I get the error:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='btn btn-default btn-lg primary-light-blue-btn']"}
I have also tried other things, but it just won't find the button. E.g.:
l = driver.find_element(By.XPATH, "//button[text()='Create report']")
I have also tried introducing a wait, but that does not help, e.g.:
l = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Create report']")))
As per the given HTML:
The element is a
<a>tag.Solution
To click on the element with text as Create report you can use either of the following locator strategies:
Using link_text:
Using css_selector:
Using xpath:
Ideally to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using LINK_TEXT:
Using CSS_SELECTOR:
Using XPATH:
Note: You have to add the following imports :