1

I´m trying to click a button inside a side bar label:

<span class="sidebar-label">
    Exportar Listagem
</span>

Here is my code:

driver.get("https://ispot2.faturaiqos.pt")
html = driver.page_source
exp = driver.find_element_by_link_text("Exportar Listagem")
exp.click()

Here is the error I get:

Traceback (most recent call last):
File "/home/pi/Desktop/Python Testes/Ispot JP.py", line 25, in <module>
exp = driver.find_element_by_link_text("Exportar Listagem")
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 317, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 745, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Exportar Listagem"}
(Session info: chrome=84.0.4147.141)
(Driver info: chromedriver=84.0.4147.141 (80c974bf7990b9735a8e885046fc5c9b1da4796c-refs/branch-heads/4147@{#1132}),platform=Linux 5.4.79-v7+ armv7l)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Peter Pan
  • 35
  • 8

1 Answers1

1

To click on the element with text as Exportar Listagem you can use the following Locator Strategies:

  • Using xpath and contains():

    driver.find_element_by_xpath("//span[@class='sidebar-label' and contains(., 'Exportar Listagem')]").click()
    
  • Using xpath and normalize-space():

    driver.find_element_by_xpath("//span[@class='sidebar-label' and normalize-space()='Exportar Listagem']").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using xpath and contains():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='sidebar-label' and contains(., 'Exportar Listagem')]"))).click()
    
  • Using xpath and normalize-space():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='sidebar-label' and normalize-space()='Exportar Listagem']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Perfect :), my next question is: i got to this "Exportar Listagem" page and now i need to fill 2 date forms before clicking the button "Exportar" Date From Date To Click Button: Exportar – Peter Pan Dec 12 '20 at 13:13
  • @PeterPan Sounds to be a different question all together. Can you raise a new question as per your new requirement please? Stackoverflow contributors will be happy to help you out. – undetected Selenium Dec 12 '20 at 13:15