从网页:
我想从下拉菜单中选择不同的年份:
<select name="ctl00$contentPlaceHolderConteudo$cmbAnos" onchange="javascript:setTimeout('AjaxNS.AR(\'ctl00$contentPlaceHolderConteudo$cmbAnos\',\'\', \'ctl00_contentPlaceHolderConteudo_AjaxPanel1\', event)', 0)" id="ctl00_contentPlaceHolderConteudo_cmbAnos">
<option selected="selected" value="2018">2018</option>
<option value="2017">2017</option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
我在互联网上做了一些研究,似乎Select
是标准方法。
下面的代码是我使用的代码;它显示(默认)2018年的网页,但它不会改变我打电话给Select
的年份。
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
url = 'http://www.bmfbovespa.com.br/pt_br/produtos/indices/indices-amplos/indice-ibovespa-ibovespa-estatisticas-historicas.htm'
driver.get(url)
select = Select(driver.find_element_by_id('ctl00$contentPlaceHolderConteudo$cmbAnos"))
最后一行产生此错误:
NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"ctl00$contentPlaceHolderConteudo$cmbAnos"}
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.38 (0),platform=Linux 4.17.19-200.fc28.x86_64 x86_64)
这似乎与下拉框菜单的名称有关。
编辑1
我已根据您的建议修改了代码。如果我使用该行:
select = Select(driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_cmbAnos"))
我收到错误:
Traceback (most recent call last):
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
execfile(filename, namespace)
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/ravas/pCloudDrive/exchange/python/fetch/untitled0.py", line 47, in <module>
select = Select(driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_cmbAnos"))
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 359, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 966, in find_element
'value': value})['value']
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"ctl00_contentPlaceHolderConteudo_cmbAnos"}
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.38 (0),platform=Linux 4.17.19-200.fc28.x86_64 x86_64)
如果我使用该行:
select = Select(driver.find_element_by_name("ctl00$contentPlaceHolderConteudo$cmbAnos")
我收到错误:
Traceback (most recent call last):
File "<ipython-input-1-35a2090c7564>", line 1, in <module>
runfile('/home/ravas/pCloudDrive/exchange/python/fetch/untitled0.py', wdir='/home/ravas/pCloudDrive/exchange/python/fetch')
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
execfile(filename, namespace)
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/ravas/pCloudDrive/exchange/python/fetch/untitled0.py", line 47, in <module>
select = Select(driver.find_element_by_name("ctl00$contentPlaceHolderConteudo$cmbAnos"))
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 495, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 966, in find_element
'value': value})['value']
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "/home/ravas/miniconda3/envs/p36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"ctl00$contentPlaceHolderConteudo$cmbAnos"}
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.38 (0),platform=Linux 4.17.19-200.fc28.x86_64 x86_64)
你使用name
属性的值代替id
,尝试:
select = Select(driver.find_element_by_id('ctl00_contentPlaceHolderConteudo_cmbAnos'))
然后您可以使用以下方法选择值
#select by visible text
select.select_by_visible_text('2013')
#select by value
select.select_by_value('2013')
根据您给定的HTML,您已使用name属性的值来定位元素。还有单双引号和右括号的语法错误。
您可以参考如下:
如果您想使用id定位器,您的语法将是:
select = Select(driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_cmbAnos")
如果要使用名称定位器,您的语法将是:
select = Select(driver.find_element_by_name("ctl00$contentPlaceHolderConteudo$cmbAnos")