Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

With Selenium and Chrome webdriver on MacOS need to click dropdown element. But always have an error that can't find. Have this html code on a page where it located:

<select id="periodoExtrato" name="periodoExtrato" class="EXTtexto" onchange="enviarExtrato(document.formperiodo.periodoExtrato[document.formperiodo.periodoExtrato.selectedIndex].value);">
<!--<option value="01" >&Uacute;ltimo dia</option>-->
<option value="03" selected="true">últimos 3 dias</option>
<option value="05">últimos 5 dias</option>
<option value="07">últimos 7 dias</option>
<option value="15">últimos 15 dias</option>
<option value="30">últimos 30 dias</option>
<option value="X">Data específica (até 60 dias)</option>
<option value="D">Mês completo (desde 2002)</option>
</select>

I need to choose últimos 15 dias, so i have this code:

self.driver.find_element_by_xpath('//[@id="periodoExtrato"]/option[4]').click()

But have an error:

Traceback (most recent call last):
  File "olxscrape.py", line 120, in <module>
    main()
  File "olxscrape.py", line 117, in main
    b = Bot()
  File "olxscrape.py", line 12, in __init__
    self.navigate()
  File "olxscrape.py", line 106, in navigate
    self.driver.find_element_by_xpath('//*[@id="periodoExtrato"]/option[4]').click()
  File "/Users/Lutchenko/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 293, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Users/Lutchenko/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
    'value': value})['value']
  File "/Users/Lutchenko/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/Users/Lutchenko/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="periodoExtrato"]/option[4]"}
  (Session info: chrome=55.0.2883.95)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Mac OS X 10.12.2 x86_64)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
197 views
Welcome To Ask or Share your Answers For Others

1 Answer

I would suggest not using xpath here. Try, instead, something like this:

 select = Select(self.driver.find_element_by_id('periodoExtrato'))
 option_indexes = range(1, len(select.options))
 for index in option_indexes:
    select.select_by_index(index)
    ......

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...