Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click on span element with python selenium

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")
like image 304
user3281831 Avatar asked Jun 20 '17 23:06

user3281831


People also ask

How do you click a span element in Selenium Python?

We can select the text of a span on click with Selenium webdriver. To identify the element with span tag, we have to first identify it with any of the locators like xpath, css, class name or tagname. After identification of the element, we can perform the click operation on it with the help of the click method.

How do you find the element of a span class?

We can locate elements in span class and not unique id with the help of the Selenium webdriver. We can identify an element having a class attribute with the help of the locator xpath, css or class name. To locate elements with these locators we have to use the By. xpath, By.

How do you click on an element at Selenium with coordinates?

We can use the ClickAt command in Selenium IDE. The ClickAt command has two arguments − the element locator and the coordinates which mentions the x and y coordinates of the mouse with respect to the element identified by the locator.


1 Answers

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()
like image 152
Andersson Avatar answered Sep 26 '22 02:09

Andersson