1

I'm having trouble performing a click operation on a website. I'm getting a error message NoSuchElementException, but I'm not sure why because I got the class name from the site.

What am I missing?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service


s = Service('C:/Program Files (x86)/chromedriver.exe')


chromeOptions = Options()
chromeOptions.headless = False
driver = webdriver.Chrome(service=s, options=chromeOptions)

list_data = []

def initialize_browser():
   driver.get("https://virtualracingschool.appspot.com/#/Home")
   print("starting_Driver")
   click_button = driver.find_element(By.CLASS_NAME, "white-text")
   driver.implicitly_wait(15)
   click_button.click()

initialize_browser()

Site & Code:

[]

I tried referencing some documents from the selenium site and it mentions for a format

<p class="content">Site content goes here.</p>` 

write the code:

content = driver.find_element(By.CLASS_NAME, 'content')`

I felt like I did this properly but my site has

<a class="white-text" style="" ...>
    <span>Login</span>
</a>

format. Is the <a> and "style" element hindering my code?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

Use the below XPath expression:

//span[text()='Login']

Your code should look like this:

click_button = driver.find_element(By.XPATH, "//span[text()='Login']")

Below is the inspect element by this XPath for your reference:

enter image description here

Shawn
  • 4,064
  • 2
  • 11
  • 23
0

As per your code trials:

click_button = driver.find_element(By.CLASS_NAME, "white-text")

By.CLASS_NAME, "white-text" identifies four elements within the HTML DOM:

white-text

Hence you see the see the error.


Solution

To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://virtualracingschool.appspot.com/#/Home')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.white-text > span"))).click()
    
  • Using XPATH:

    driver.get('https://virtualracingschool.appspot.com/#/Home')  
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='white-text']//span"))).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
    
  • Browser snapshot:

virtualracingschool

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352