javascript - 使用 Selenium 收集 Javascript 下拉菜单上的每个选项
问题描述
我正在尝试使用 Selenium 收集此页面所有可能版本(级别、事件和季节的所有组合)的 URL。我已经成功地使用driver.find_elements_by_xpath
导航到正确的选项并在保存 URL 之前单击它,但这非常慢,我想知道是否有更好的选择。
href
如果不单击实际选项,我似乎没有任何属性可以窃取链接。使用Select
类并尝试遍历选项更干净,但我仍然必须Select
每次都生成对象 - 尝试这样做:
s = Select(driver.find_element_by_xpath("//label[contains(text(), 'Level')]/../select"))
for option in s.options:
option.click()
适用于第一个选项,但随后给了我错误stale element reference: element is not attached to the page document
。我很难过 - 有没有更好的方法来收集这些链接?下面是我的代码片段:
driver.get("https://athletic.net/TrackAndField/Division/Event.aspx?DivID=89120&Event=1")
for i in range(0, len(driver.find_elements_by_xpath("//label[contains(text(), 'Level')]/../select/option"))):
driver.find_elements_by_xpath("//label[contains(text(), 'Level')]/../select/option")[i].click()
for j in range(0, len(driver.find_elements_by_xpath("//optgroup//option[contains(text(), 'Meters')]"))):
driver.find_elements_by_xpath("//optgroup//option[contains(text(), 'Meters')]")[j].click()
for k in range(0, len(driver.find_elements_by_xpath("//label[contains(text(), 'Season')]/..//option[contains(text(), 'Indoor')]/../option"))):
driver.find_elements_by_xpath("//label[contains(text(), 'Season')]/..//option[contains(text(), 'Indoor')]/../option")[k].click()
for l in range(0, len(driver.find_elements_by_xpath("//label[contains(text(), 'Season')]/..//option[contains(text(), '2018')]/../option"))):
driver.find_elements_by_xpath("//label[contains(text(), 'Season')]/..//option[contains(text(), '2018')]/../option")[l].click()
with open("links.txt", 'a+') as f:
f.write(driver.current_url + ";")
解决方案
URL 是Location
由“DivID”Event
标识和由“Event”标识的组合。
因此,您可以使用find_elements_by_xpath
(复数)查找options
两个下拉列表的所有内容,然后使用列表理解value
从每个下拉列表中提取属性option
location_option_list = driver.find_elements_by_xpath("//select[@ng-model='appC.locationDivId']/option")
location_values = [location_option.get_attribute('value') for location_option in location_option_list]
event_option_list = driver.find_elements_by_xpath("//select[@ng-model='appC.params.eventId']//option")
event_values = [event_option.get_attribute('value') for event_option in event_option_list]
urls = ""
for location_value in location_values:
for event_value in event_values:
urls += "https://www.athletic.net/TrackAndField/Division/Event.aspx?DivID=%s&Event=%s;" \
% (location_value, event_value)
上述代码仅适用于“高中”和“中学”级别。您可以轻松修改它以处理“青年俱乐部”和“大学”级别
推荐阅读
- javascript - 使用 Promise all 填充下拉列表
- java - 如何使用spring data jpa删除特定的语句块
- performance-testing - 可以以无代码方式将 Azure 应用程序洞察与现有服务结构集群集成吗?
- vim - VimScript - 即使在缓冲区更改后也始终如一地保存特定位置
- python - 生成 Pandas 数据框的排列
- .net-core - .net 标准兼容性
- java - 我们能知道文本是否水平超出了StaticLayout的边界,单行文本,垂直多行文本
- java - 如何在 Android 上使用桌面后备 URL 构建简短的动态链接?
- go - 使用多个 goroutine 的好处
- javascript - 在输入中搜索会在材料日期选择器中给出不正确的日期,需要将其转换为 UTC