【问题标题】:How to use Selenium to get the parking price?如何使用 Selenium 获取停车价格?
【发布时间】:2016-05-06 07:49:34
【问题描述】:

我正在尝试使用网络抓取来获取此链接上的停车价格,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)

【问题讨论】:

    标签: python selenium web web-scraping screen-scraping


    【解决方案1】:

    页面加载需要时间。在 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 - $2.00"
    

    【讨论】:

    • 感谢您的大力帮助。有用。其实我想知道等待是原因。所以我尝试了 driver.wait = WebDriverWait(driver, 16) 或 driver.implicitly_wait(20),但没有奏效。你知道为什么在这种情况下显式或隐式等待不起作用吗?
    • @teapot 我认为这是一个很好的概述:stackoverflow.com/questions/10404160/…。另外,请参阅stackoverflow.com/help/someone-answers
    • 当然我会接受这个答案。我只需要等待至少 10 分钟即可单击该按钮。你真棒!感谢您提供概述链接,我会看看。
    • 糟糕,修改无效。第一个答案虽然有效。
    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    相关资源
    最近更新 更多