I'm trying to use web scraping to get the parking price at this link, https://application.parkbytext.com/accountus/prepay.htm?locationCode=1127. It's $2 for the day which is what I'm trying to get. I'm using python+selenium, but just couldn't get the parking price. Below is the code I'm using, but sometimes I hit the target but most time I get the error
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"class name","selector":"gwt-RadioButton"}.
Can anyone help? thanks in advance
def downtownparking(driver):
driver.get("https://application.parkbytext.com/accountus/prepay.htm?locationCode=1127")
try:
### driver.wait = WebDriverWait(driver, 16)
### driver.implicitly_wait(20)
cr = driver.find_element_by_class_name("gwt-RadioButton")
dayprice = cr.find_element_by_tag_name("label")
print (dayprice.text)
The page loading takes time. At the moment webdriver tries to find an element, it is not yet present in the DOM tree. Add an Explicit Wait:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
cr = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "gwt-RadioButton"))
)
As a side, note, I would use the input
's name instead:
cr = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//input[@name='periodType']/following-sibling::label"))
)
print(cr.text) # prints "Day - $2.00"