Selenium WebDriver可以从下拉菜单中单击所有组合吗? [英] Can Selenium WebDriver click on all combinations from drop down menu

查看:82
本文介绍了Selenium WebDriver可以从下拉菜单中单击所有组合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上,我需要从UCLA书店网站获取部门,课程和部门的所有组合: http ://shop.uclastore.com/courselistbuilder.aspx ,然后我需要选择书籍并解析生成的html页面.

So basically I need to get all combinations of Department, Course, and Section from the UCLA bookstore site: http://shop.uclastore.com/courselistbuilder.aspx and then I need to choose the books and parse the resulting html page.

我正在寻找其他选项以编程方式进行操作,而不是手动进行操作(这将永远花费时间).我找到的一个选择是Selenium WebDriver,它处理浏览器自动化.从SO的示例中,我发现Selenium WebDriver是一个很有前途的功能,但是我不确定它是否能够满足我的需要.

Rather than doing it manually (which would take forever), I was looking up other options to do it programmatically. One option I found is Selenium WebDriver which deals with browser automation. Looking at examples from SO I find that the Selenium WebDriver is a promising feature but I am not quite sure if it can be able to do what I need it to.

或多或少采用伪代码格式,这是我如何使用Selenium Web驱动程序的方法

More or less in pseudocode format, here is my approach on how I am going to use selenium web driver

go to the site: http://shop.uclastore.com/courselistbuilder.aspx

for each_department in department:
    click on each_department
    for each_course in course:
        click on each_course
            for each_section in section:
                click on each_section
// After every department, course, and section has been chosen, we click choose books
click on choose books link

// Save the resulting html file
save next page as html file

我想知道我是否能够使用Selenium WebDriver做我想做的事情.如果有人可以提供更好的伪代码,将其与Selenium WebDriver更合适地绑定,这将很有帮助,但是我主要关注的是是否可以实现此功能.我还想提到我计划在使用Selenium时使用Python API.

I wanted to know if I can be able to do what I want using Selenium WebDriver. It would be helpful if someone could offer better pseudocode that would be tied more appropriately with Selenium WebDriver but I am mainly focused on if this functionality could be possible. I also wanted to mention that I am planning to use the Python API when using Selenium.

推荐答案

您应该从此处开始(以下说明):

This is there you should start (explanation below):

from selenium import webdriver
import time
from selenium.webdriver.support.select import Select


url = "http://shop.uclastore.com/courselistbuilder.aspx"

driver = webdriver.Firefox()
driver.get(url)
time.sleep(1)

departments = Select(driver.find_element_by_id('clDeptSelectBox'))
for department in departments.options:
    # select department
    departments.select_by_value(department.get_attribute('value'))
    time.sleep(1)

    cources = Select(driver.find_element_by_id('clCourseSelectBox'))
    for cource in cources.options:
        # select course
        cources.select_by_value(cource.get_attribute('value'))
        time.sleep(1)

        sections = Select(driver.find_element_by_id('clSectionSelectBox'))
        for section in sections.options:
            print {'department': department.text,
                   'course': cource.text,
                   'section': section.text}

driver.close()

打印:

{'department': u'AFRCST - AFRICAN STUDIES', 'course': u'201A', 'section': u'1 - LYDON'}
{'department': u'AFRKLA - AFRICAN LANGUAGES', 'course': u'1A', 'section': u'1 - TA'}
{'department': u'AFRKLA - AFRICAN LANGUAGES', 'course': u'150A', 'section': u'1 - FANTA, A.A.'}
{'department': u'AFROAM - AFRO-AMERICAN STUDIES', 'course': u'6', 'section': u'1 - STREETER, C.A.'}
{'department': u'AFROAM - AFRO-AMERICAN STUDIES', 'course': u'M10A', 'section': u'1 - LYDON, G.E.'}
{'department': u'AFROAM - AFRO-AMERICAN STUDIES', 'course': u'M102', 'section': u'1 - LEWIS, L.I.'}
{'department': u'AFROAM - AFRO-AMERICAN STUDIES', 'course': u'M103A', 'section': u'1 - PRICE, Z.F.'}
{'department': u'AFROAM - AFRO-AMERICAN STUDIES', 'course': u'M104A', 'section': u'1 - YARBOROUGH'}
...

这个想法是广泛使用 Selectselect/options功能提供了很好的API.首先,我们要获得所有部门,然后遍历选项并在循环中选择下一个部门.然后,经过一小段延迟后,我们以相同的方式获取coursessections的列表.

The idea is to extensively use Select class that provides a nice API the select/options functionality. First, we are getting all departments, then iterating over the options and choose the next department in the loop. Then, after the small delay, we are getting the list of courses and sections in the same manner.

我已为您更好地处理等待(time.sleep()不太可靠),然后点击Choose Books按钮(好吧,德国彼得罗夫为您提供了两者).

I've left for you a better handling of Waits (time.sleep() really is not very reliable) and clicking on Choose Books button (well, German Petrov provided you with both).

希望有帮助.

这篇关于Selenium WebDriver可以从下拉菜单中单击所有组合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆