我正在从WTO抓取一些数据,我需要从下拉菜单中选择进口商。对于进口商137(越南),我的代码找到菜单,选择索引137并显示数据。但是,我也希望我的代码打印与索引137(越南)相对应的标签或国家/地区,但是我正在为此类任务而苦苦挣扎。
这基本上是我必须选择导入器137的地方:
importer=137
time.sleep(1)
mySelect = Select(driver.find_element_by_id("ctl00_c_cboImporter"))
mySelect.select_by_index(importer)
这是菜单的html:
<select name="ctl00$c$cboExporter" onchange="javascript:setTimeout('__doPostBack(\'ctl00$c$cboExporter\',\'\')', 0)" id="ctl00_c_cboExporter" class="ListControl" style="width:100%;font-weight:normal;font-size:11px;font-family:Tahoma,Arial,Helvetica,sans-serif;color:#333399;border-width:1px;border-style:Solid;border-color:#6291CC;background-color:White;">
<option value="A000">Select an Exporter</option>
<option selected="selected" value="C004">Afghanistan</option>
<option value="C008">Albania</option>
<option value="C012">Algeria</option>
<option value="C020">Andorra</option>
<option value="C024">Angola</option>
<option value="C028">Antigua and Barbuda</option>
<option value="C032">Argentina</option>
<option value="C051">Armenia</option>
<option value="C036">Australia</option>
<option value="C031">Azerbaijan</option>
<option value="C044">Bahamas</option>
<option value="C048">Bahrain, Kingdom of</option>
<option value="C050">Bangladesh</option>
<option value="C052">Barbados</option>
<option value="C112">Belarus</option>
<option value="C084">Belize</option>
<option value="C204">Benin</option>
<option value="C064">Bhutan</option>
<option value="C068">Bolivia, Plurinational State of</option>
<option value="C070">Bosnia and Herzegovina</option>
<option value="C072">Botswana</option>
<option value="C076">Brazil</option>
<option value="C096">Brunei Darussalam</option>
<option value="C854">Burkina Faso</option>
<option value="C108">Burundi</option>
<option value="C132">Cabo Verde</option>
<option value="C116">Cambodia</option>
<option value="C120">Cameroon</option>
<option value="C124">Canada</option>
<option value="C140">Central African Republic</option>
<option value="C148">Chad</option>
<option value="C152">Chile</option>
<option value="C156">China</option>
<option value="C170">Colombia</option>
<option value="C174">Comoros</option>
<option value="C178">Congo</option>
<option value="C188">Costa Rica</option>
<option value="C384">Côte d'Ivoire</option>
<option value="C192">Cuba</option>
<option value="C180">Democratic Republic of the Congo</option>
<option value="C262">Djibouti</option>
<option value="C212">Dominica</option>
<option value="C214">Dominican Republic</option>
<option value="C218">Ecuador</option>
<option value="C818">Egypt</option>
<option value="C222">El Salvador</option>
<option value="C226">Equatorial Guinea</option>
<option value="C748">Eswatini</option>
<option value="C231">Ethiopia</option>
<option value="U918">European Union</option>
<option value="C242">Fiji</option>
<option value="C266">Gabon</option>
<option value="C270">The Gambia</option>
<option value="C268">Georgia</option>
<option value="C288">Ghana</option>
<option value="C308">Grenada</option>
<option value="C320">Guatemala</option>
<option value="C324">Guinea</option>
<option value="C624">Guinea-Bissau</option>
<option value="C328">Guyana</option>
<option value="C332">Haiti</option>
<option value="C340">Honduras</option>
<option value="C344">Hong Kong, China</option>
<option value="C352">Iceland</option>
<option value="C356">India</option>
<option value="C360">Indonesia</option>
<option value="C364">Iran</option>
<option value="C368">Iraq</option>
<option value="C376">Israel</option>
<option value="C388">Jamaica</option>
<option value="C392">Japan</option>
<option value="C400">Jordan</option>
<option value="C398">Kazakhstan</option>
<option value="C404">Kenya</option>
<option value="C410">Korea, Republic of</option>
<option value="C414">Kuwait, the State of</option>
<option value="C417">Kyrgyz Republic</option>
<option value="C418">Lao People's Democratic Republic</option>
<option value="C422">Lebanese Republic</option>
<option value="C426">Lesotho</option>
<option value="C430">Liberia</option>
<option value="C434">Libya</option>
<option value="C438">Liechtenstein</option>
<option value="C446">Macao, China</option>
<option value="C450">Madagascar</option>
<option value="C454">Malawi</option>
<option value="C458">Malaysia</option>
<option value="C462">Maldives</option>
<option value="C466">Mali</option>
<option value="C478">Mauritania</option>
<option value="C480">Mauritius</option>
<option value="C484">Mexico</option>
<option value="C498">Moldova, Republic of</option>
<option value="C496">Mongolia</option>
<option value="C893">Montenegro</option>
<option value="C504">Morocco</option>
<option value="C508">Mozambique</option>
<option value="C104">Myanmar</option>
<option value="C516">Namibia</option>
<option value="C524">Nepal</option>
<option value="C554">New Zealand</option>
<option value="C558">Nicaragua</option>
<option value="C562">Niger</option>
<option value="C566">Nigeria</option>
<option value="C807">North Macedonia</option>
<option value="C578">Norway</option>
<option value="C512">Oman</option>
<option value="C586">Pakistan</option>
<option value="C591">Panama</option>
<option value="C598">Papua New Guinea</option>
<option value="C600">Paraguay</option>
<option value="C604">Peru</option>
<option value="C608">Philippines</option>
<option value="C634">Qatar</option>
<option value="C643">Russian Federation</option>
<option value="C646">Rwanda</option>
<option value="C659">Saint Kitts and Nevis</option>
<option value="C662">Saint Lucia</option>
<option value="C670">Saint Vincent and the Grenadines</option>
<option value="C882">Samoa</option>
<option value="C678">Sao Tomé and Principe</option>
<option value="C682">Saudi Arabia, Kingdom of</option>
<option value="C686">Senegal</option>
<option value="C892">Serbia</option>
<option value="C690">Seychelles</option>
<option value="C694">Sierra Leone</option>
<option value="C702">Singapore</option>
<option value="C090">Solomon Islands</option>
<option value="C706">Somalia</option>
<option value="C710">South Africa</option>
<option value="C728">South Sudan</option>
<option value="C144">Sri Lanka</option>
<option value="C736">Sudan</option>
<option value="C740">Suriname</option>
<option value="C756">Switzerland</option>
<option value="C760">Syrian Arab Republic</option>
<option value="C158">Chinese Taipei</option>
<option value="C762">Tajikistan</option>
<option value="C834">Tanzania</option>
<option value="C764">Thailand</option>
<option value="C626">Timor-Leste</option>
<option value="C768">Togo</option>
<option value="C776">Tonga</option>
<option value="C780">Trinidad and Tobago</option>
<option value="C788">Tunisia</option>
<option value="C792">Turkey</option>
<option value="C800">Uganda</option>
<option value="C804">Ukraine</option>
<option value="C784">United Arab Emirates</option>
<option value="C840">United States of America</option>
<option value="C858">Uruguay</option>
<option value="C860">Uzbekistan</option>
<option value="C548">Vanuatu</option>
<option value="C862">Venezuela, Bolivarian Republic of</option>
<option value="C704">Viet Nam</option>
<option value="C887">Yemen</option>
<option value="C894">Zambia</option>
<option value="C716">Zimbabwe</option>
选择进口商137之后,我希望我的代码根据所选下拉菜单中的选项找到与进口商137相对应的国家/地区。
谢谢!
答案 0 :(得分:1)
您可以使用first_selected_option
返回当前选择的
selected = mySelect.first_selected_option
print(selected.text)
示例:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
url = 'http://www.debentures.com.br/exploreosnd/consultaadados/sndemumclique/'
driver = webdriver.Chrome()
driver.get(url)
options = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[name="ctl00$ddlEmi"] option')))
importer = 3
mySelect = Select(driver.find_element_by_css_selector('[name="ctl00$ddlEmi"]'))
mySelect.select_by_index(importer)
selected = mySelect.first_selected_option
print(selected.text)
您可以使用attribute = value css选择器来获取option元素,然后从中获取.text
label = driver.find_element_by_css_selector('[value="C704"]').text
但是您可以避免索引并按文本选择
select.select_by_visible_text('Viet Nam')
或抓住所有选项
options = driver.find_elements_by_css_selector('[name="ctl00$c$cboExporter"] option')
然后索引到其中并使用.text返回关联的值
示例:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'http://www.debentures.com.br/exploreosnd/consultaadados/sndemumclique/'
driver = webdriver.Chrome()
driver.get(url)
options = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[name="ctl00$ddlEmi"] option')))
options[5].click()
print(options[5].text)
如果您想尝试全部循环,则可能类似
options = driver.find_elements_by_css_selector('[name="ctl00$c$cboExporter"] option')
for option in options:
option.click()
print(option.text)
这是一个示例循环
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'http://www.debentures.com.br/exploreosnd/consultaadados/sndemumclique/'
driver = webdriver.Chrome()
driver.get(url)
options = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[name="ctl00$ddlEmi"] option')))
for option in options[1:]:
option.click()
print(option.text)
答案 1 :(得分:0)
在单击该选项之前,您可以找到它并获取其文本:
mySelect = Select(driver.find_element_by_id("ctl00_c_cboImporter"))
# with css
text = mySelect.find_element_by_css("option:nth-of-type({})".format(importer)).text
# xpath
text = mySelect.find_element_by_xpath("option[{}])".format(importer)).text
mySelect.select_by_index(importer)