简体   繁体   中英

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). These are imported from a web platform, which is of restricted access. 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:

<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:

<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 .

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() . Try selecting by index instead.

It's also strange that your select html element doesn't have quotes surrounding the id . It's possible that selenium isn't able to find it using 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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