Reputation: 656
From the webpage:
I'm trying to select different years from the drop-down-menu:
<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>
I have done some research on the the internet, and it seems that Select
is the standard approach.
The code below is the one I use; it displays the webpage for the (default) year 2018, but it doesn't change the year when I call 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"))
The last line produces this error:
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)
which seems to be associated with the name of the drop-box-menu.
EDIT 1
I have modified the code following your advice. If I use the line:
select = Select(driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_cmbAnos"))
I get the error:
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)
If I use the line:
select = Select(driver.find_element_by_name("ctl00$contentPlaceHolderConteudo$cmbAnos")
I get the error:
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)
Upvotes: 0
Views: 632
Reputation: 4035
As according to your given HTML, You have used value of name attribute to locate element. Also its Syntax Error with Single-Double Quotes and closing bracket.
You may Refer as following:
If you want to use id locator, Your Syntax will be :
select = Select(driver.find_element_by_id("ctl00_contentPlaceHolderConteudo_cmbAnos")
If you want to use name locator, Your Syntax will be :
select = Select(driver.find_element_by_name("ctl00$contentPlaceHolderConteudo$cmbAnos")
Upvotes: 0
Reputation: 693
you are using value of name
attribute in place of id
, try:
select = Select(driver.find_element_by_id('ctl00_contentPlaceHolderConteudo_cmbAnos'))
And then you can use below method to select values
#select by visible text
select.select_by_visible_text('2013')
#select by value
select.select_by_value('2013')
Upvotes: 1