简体   繁体   English

如何在网络下拉框中选择选项?

[英]How to choose option on a web drop down box?

I'm using Python 3.5.1, and I need to automate the download of a bunch of reports every morning (through Python). 我正在使用Python 3.5.1,并且需要每天早上(通过Python)自动下载一堆报告。 These are imported from a web platform, which is of restricted access. 这些是从受限制访问的Web平台导入的。 I started with the following piece of code, which works fine: 我从下面的代码开始,这很好用:

import webbrowser, time
webbrowser.open('mylink')
time.sleep(10)

However, I'm struggling now with a drop down box, from which I have to choose and submit the location. 但是,我现在正努力使用一个下拉框,必须从中选择并提交位置。 Here's the HTML necessary to solve the problem: 这是解决问题所需的HTML:

<SELECT id=PRMT_SV_N000000000C88F800x000000000E8040B8_NS_ class="clsSelectControl pv" aria-invalid=false style="WIDTH: auto" hasLabel="true">
  <OPTION selected>MDM Location Code</OPTION>
  <OPTION>--------------------------------------------</OPTION>
  <OPTION value=3002 dv="3002  Ontario 002">3002&nbsp;&nbsp;Ontario&nbsp;&nbsp;002</OPTION>
  <OPTION value=3004 dv="3004  Fresno, CA  004">3004&nbsp;&nbsp;Fresno,&nbsp;CA&nbsp;&nbsp;004</OPTION>
  <OPTION value=3005 dv="3005  San Diego

The Finish/submit button HTML: “完成/提交”按钮的HTML:

<BUTTON onclick="oCV_NS_.promptAction('finish')" onmouseover="this.className = 'bp bph'" onmouseout="this.className = 'bp'" id=finishN000000000C88F800x000000000E804A58_NS_ class=bp name=finishN000000000C88F800x000000000E804A58_NS_>Finish</BUTTON>

I have omitted most of the options of course. 我当然省略了大多数选择。 For this exercise, imagine that I want to choose the first one, 3004 Fresno, CA 004 . 对于本练习,假设我想选择第一个, 3004 Fresno, CA 004

I'm assuming they don't work because you're trying to select using the wrong text or value using select_by_value() or select_by_visible_text() . 我假设它们不起作用,因为您正尝试使用select_by_value()select_by_visible_text()使用错误的文本或值进行选择。 Try selecting by index instead. 请尝试按索引选择。

It's also strange that your select html element doesn't have quotes surrounding the id . select html元素在id周围没有引号也很奇怪。 It's possible that selenium isn't able to find it using find_element_by_id . 硒可能无法使用find_element_by_id找到它。

Try this: 尝试这个:

from selenium import webdriver
from selenium.webdriver.support.ui import Select

...

webbrowser.open('mylink')

dropdown = driver.find_element_by_id("PRMT_SV_N000000000C88F800x000000000E8040B8_NS_")    
select = Select(dropdown)    
select.select_by_index(3) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM