如何使用Selenium获取停车费?
How to use Selenium to get the parking price?
我正在尝试使用网络抓取来获取 link、https://application.parkbytext.com/accountus/prepay.htm?locationCode=1127 的停车价格。当天的价格是 $2,这就是我想要得到的。我正在使用 python+selenium,但无法获取停车费。下面是我正在使用的代码,但有时我会击中目标,但大多数时候我会收到错误
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"class name","selector":"gwt-RadioButton"}.
有人可以帮忙吗?提前致谢
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)
页面加载需要时间。目前 webdriver 试图找到一个元素,它还没有出现在 DOM 树中。添加一个 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"))
)
顺便说一句,请注意,我会改用 input
的名字:
cr = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//input[@name='periodType']/following-sibling::label"))
)
print(cr.text) # prints "Day - .00"
我正在尝试使用网络抓取来获取 link、https://application.parkbytext.com/accountus/prepay.htm?locationCode=1127 的停车价格。当天的价格是 $2,这就是我想要得到的。我正在使用 python+selenium,但无法获取停车费。下面是我正在使用的代码,但有时我会击中目标,但大多数时候我会收到错误
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"class name","selector":"gwt-RadioButton"}.
有人可以帮忙吗?提前致谢
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)
页面加载需要时间。目前 webdriver 试图找到一个元素,它还没有出现在 DOM 树中。添加一个 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"))
)
顺便说一句,请注意,我会改用 input
的名字:
cr = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//input[@name='periodType']/following-sibling::label"))
)
print(cr.text) # prints "Day - .00"