How to click on span element with python selenium
Search by link text can help you only if your span
is a child of anchor tag, e.g. <a><span style="vertical-align: middle;">No</span></a>
. As you're trying to click it, I believe it's really inside an anchor, but if not I'd suggest you to use XPath
with predicate that returns True
only if exact text content matched:
//span[text()="No"]
Note that //span[contains(text(), "No")]
is quite unreliable solution as it will return span
elements with text
- "November rain"
- "Yes. No."
- "I think Chuck Norris can help you"
etc...
If you get NoSuchElementException
you might need to wait for element to appear in DOM
:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='No']"))).click()