gpt4 book ai didi

python - Selenium select_by_value 然后打印选项文本

转载 作者:行者123 更新时间:2023-12-01 13:15:13 25 4
gpt4 key购买 nike

我正在从 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 对应的国家/地区。

谢谢!

最佳答案

您可以使用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 选择器获取选项元素,然后从中获取 .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)

关于python - Selenium select_by_value 然后打印选项文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55735903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com