I am trying to use selenium to load a waiting list and click the button, but I can't seem to find the element

Viewed 71

I've tried to use find_element_by_class_name and link text and both result in NoSuchElement Exception. I'm just trying to click this Join waitlist button for https://v2.waitwhile.com/l/fostersbarbershop/list-view - Any assistance is greatly appreciated.

from selenium import webdriver
import time

PATH = "C:\Python\Pycharm\attempt\Drivers\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://v2.waitwhile.com/l/fostersbarbershop/list-view")

join = False

while not join:

    try:
        joinButton = driver.find_element_by_class_name("disabled")
        print("Button isnt ready yet.")
        time.sleep(2)
        driver.refresh()

    except:
        joinButton = driver.find_element_by_class_name("public-submit-btn")
        print("Join")
        joinButton.click()
        join = True
2 Answers

The page your trying to automate is angular. Typically what happens in script-based pages is you download the source, the page load event is classed as complete, then some JS scripts run to get/update the page content. Those scripts (which can take seconds to complete) update the DOM with the page you see.

In contrast selenium can only recognise the page is loaded - Selenium will be attempting to find those elements at this readystate-point and is unaware of any running scripts.

You will need to wait for your element to be ready before you proceed.

Easy solution is to add an implicit wait to your script. An implicit wait will ignore the nosuchelement exception until the timeout has been reached.

See here for more information on waits.

This is the line of code to add:

driver.implicitly_wait(10)

Just adjust the time based on what you need.

You only need it once.

This is the code that works for me:

driver = webdriver.Chrome()

driver.get("https://v2.waitwhile.com/l/fostersbarbershop/list-view")
driver.implicitly_wait(10)

join = False
while not join:
    try:
        joinButton = driver.find_element_by_class_name("disabled")
        print("Button isnt ready yet.")
        time.sleep(2)
        driver.refresh()
    except:
        joinButton = driver.find_element_by_class_name("public-submit-btn")
        print("Join")
        joinButton.click()
        join = True

This is the end state: enter image description here

It seems you have synchronization issue.

Induce WebDriverWait() and wait for element_to_be_clickable() for following ID locator

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "join-waitlist"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "ww-name"))).send_keys("TestUser")

You need to import below libraries.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

browser snapshot

enter image description here

Related