How to click on span element with python selenium
Question:
Im trying to click on this for whole day with python selenium with no luck, tried several selectors, xpath..nothing seems to be work for me.
This is the element I try to click on:
<span style="vertical-align: middle;">No</span>
Here is my obviously non function code
driver.find_element_by_link_text("No")
Answers:
I don’t know if there are other elements or anything.
You can select the span using css selector:
driver.find_element_by_css_selector('span').click()
I’m considering this is the only span tag in your code. Probably it isn’t.
As a very “shotgun” solution. You could click on every span tag:
for element in driver.find_elements_by_css_selector('span'):
element.click()
Not a perfect solution, but it might be a starting point.
Other question. Is the page loading the contents dynamically? Like xhr and stuff? Perhaps you may need to make selenium wait a little.
To make selenium wait, you can use this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait_time = 60 # a very long wait time
element = WebDriverWait(driver, wait_time).
until(EC.element_to_be_clickable((By.LINK_TEXT, 'No')))
element.click()
Or you can use By.CSS_SELECTOR
instead of By.LINK_TEXT
. So you can use more specific rules to match or any of these other selections methods here.
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()
I was doing something in my project too for Spotify. This is the function I wrote to select genders which are in span html tags.
Libraries Required
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random
Defining Variables
gender_male = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Male']")))
gender_female = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Female']")))
non_binary = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-binary']")))
other = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Other']")))
pnts = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Prefer not to say']")))
Using Random
gender_guess = random.randint(1, 5)
if gender_guess == 1:
gender_male.click()
elif gender_guess == 2:
gender_female.click()
elif gender_guess == 3:
non_binary.click()
elif gender_guess == 4:
other.click()
elif gender_guess == 5:
pnts.click()
Full Code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
gender_male = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Male']")))
gender_female = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Female']")))
non_binary = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-binary']")))
other = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Other']")))
pnts = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Prefer not to say']")))
gender_guess = random.randint(1, 5)
if gender_guess == 1:
gender_male.click()
elif gender_guess == 2:
gender_female.click()
elif gender_guess == 3:
non_binary.click()
elif gender_guess == 4:
other.click()
elif gender_guess == 5:
pnts.click()
Hopefully this helped.
Im trying to click on this for whole day with python selenium with no luck, tried several selectors, xpath..nothing seems to be work for me.
This is the element I try to click on:
<span style="vertical-align: middle;">No</span>
Here is my obviously non function code
driver.find_element_by_link_text("No")
I don’t know if there are other elements or anything.
You can select the span using css selector:
driver.find_element_by_css_selector('span').click()
I’m considering this is the only span tag in your code. Probably it isn’t.
As a very “shotgun” solution. You could click on every span tag:
for element in driver.find_elements_by_css_selector('span'):
element.click()
Not a perfect solution, but it might be a starting point.
Other question. Is the page loading the contents dynamically? Like xhr and stuff? Perhaps you may need to make selenium wait a little.
To make selenium wait, you can use this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait_time = 60 # a very long wait time
element = WebDriverWait(driver, wait_time).
until(EC.element_to_be_clickable((By.LINK_TEXT, 'No')))
element.click()
Or you can use By.CSS_SELECTOR
instead of By.LINK_TEXT
. So you can use more specific rules to match or any of these other selections methods here.
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()
I was doing something in my project too for Spotify. This is the function I wrote to select genders which are in span html tags.
Libraries Required
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random
Defining Variables
gender_male = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Male']")))
gender_female = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Female']")))
non_binary = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-binary']")))
other = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Other']")))
pnts = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Prefer not to say']")))
Using Random
gender_guess = random.randint(1, 5)
if gender_guess == 1:
gender_male.click()
elif gender_guess == 2:
gender_female.click()
elif gender_guess == 3:
non_binary.click()
elif gender_guess == 4:
other.click()
elif gender_guess == 5:
pnts.click()
Full Code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
gender_male = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Male']")))
gender_female = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Female']")))
non_binary = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-binary']")))
other = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Other']")))
pnts = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Prefer not to say']")))
gender_guess = random.randint(1, 5)
if gender_guess == 1:
gender_male.click()
elif gender_guess == 2:
gender_female.click()
elif gender_guess == 3:
non_binary.click()
elif gender_guess == 4:
other.click()
elif gender_guess == 5:
pnts.click()
Hopefully this helped.