【问题标题】:Python Selenium - Select list item from unordered listPython Selenium - 从无序列表中选择列表项
【发布时间】:2021-02-05 06:42:30
【问题描述】:

网址:https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml'

我正在尝试使用 selenium 从该网站下载数据,但它的设置方式令人困惑。我需要弄清楚如何使用名为“Y-Axis”的列表中的下拉菜单并从该列表中选择“Maker”。然后我需要点击“刷新”按钮和“下载 excel”按钮。这是下拉菜单的html:

<select id="yaxisVar_input" name="yaxisVar_input" tabindex="-1" aria-hidden="true" onchange="PrimeFaces.ab({s:&quot;yaxisVar&quot;,e:&quot;change&quot;,f:&quot;masterLayout_formlogin&quot;,p:&quot;yaxisVar&quot;,u:&quot;xaxisVar&quot;});"><option value="Vehicle Category" data-escape="true">Vehicle Category</option><option value="Vehicle Class" selected="selected" data-escape="true">Vehicle Class</option><option value="Norms" data-escape="true">Norms</option><option value="Fuel" data-escape="true">Fuel</option><option value="Maker" data-escape="true">Maker</option></select>

这是我正在玩的代码:

从硒导入网络驱动程序

from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('C:/Users/abhay.singh/chromedriver')
driver.get('https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml')

# Get the y-axis selector
# select = Select(driver.find_element_by_id('yaxisVar_input'))
# select.select_by_visible_text('Maker').click()
# print(select.options)
# print([o.text for o in select.options])

driver.find_element_by_xpath("//select[@name='yaxisVar_input']/option[text()='Maker']").click()

感谢您帮助解决这个问题!

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver


    【解决方案1】:

    这是我能做的最好的。需要注意的一点是,selenium 并不是真正用于下载的,所以我在最后添加了一个 sleep 以确保下载完成。也可以用脚本来监控下载状态,但我真的不知道怎么做。我还必须在中间添加一个 sleep 以确保正确捕获“Maker”点击。我敢肯定还有更好的方法来做到这一点。

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time
    with webdriver.Chrome() as driver:
        driver.get("https://vahan.parivahan.gov.in/vahan4dashboard/vahan/view/reportview.xhtml")
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[id='yaxisVar_label']"))).click()
        time.sleep(2)
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li[data-label='Maker']"))).click()
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "j_idt61"))).click()
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "vchgroupTable:xls"))).click()
        time.sleep(10)
    

    您也许可以通过requests.post() 执行此操作。我没有查看标题或表单数据,但它就在那里。

    【讨论】:

    • 谢谢你,它非常有帮助!您知道是否还有其他方法可以使用这些过滤器获取表数据?本质上,我需要根据 States & RTO 过滤器对这些数据进行一些不同的切割,然后用 pandas 清理它们
    • 很遗憾我没有。您可以考虑发出 post 请求,或者使用可以解析 js 的不同类型的请求库,例如 requests-html。
    • 不是将WebDriverWait(driver, 10) 放在每一行,而是声明一个变量wait = WebDriverWait(driver, 10),然后使用wait.until(...) 清理代码。您的第一个定位器只是您放入 CSS 选择器中的 ID。相反,只需使用(By.ID, 'yaxisVar_label')time.sleep() 是一种非常糟糕的做法,应该删除。无论如何,您包含的WebDriverWaits 不需要它们。
    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 2013-10-02
    • 2021-05-24
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多