Search code examples
pythonseleniumxpathcss-selectorsxpath-1.0

How to click on the element with Selenium Python


I'm trying to click a the element with text as I don't have the telephone on this website.

So I find the element with inspect. here is the element in html:

<span class="toggle-link link_has-no-phone" role="button">I&nbsp;don't have a&nbsp;telephone number</span>

In my nonfunctional code i wrote this:

r = driver.find_element_by_xpath("//*[@id='root']/div/div[2]/div/main/div/div/div/form/div[3]/div/div[2]/div/span")
r.click

The button is never clicked and nothing happens i get no error and i can't click it any help would be appreciated.


Solution

  • To click on the element with text as I don't have a telephone number you can use either of the Locator Strategies:

    • css_selector:

      driver.find_element_by_css_selector("span.toggle-link.link_has-no-phone").click()
      
    • xpath:

      driver.find_element_by_xpath("//span[@class='toggle-link link_has-no-phone']").click()