Alejandro Simkievich Alejandro Simkievich - 1 month ago 9
Python Question

python - having a hard time on selenium to click on buton

I am working with selenium (python) and I would like for it to click on a button. The code goes something like this:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get('my_url')

try:
driver.find_element_by_partial_link_text('Mais').click()
except:
pass


Nothing at all happens. I do

html_source = driver.page_source


and inspect the source, and I am pretty sure this is the element I want to find:

<input class="ksb _kvc" value="Mais resultados" id="smb" data-lt="Carregando..." jsaction="str.smr" data-ved="0ahUKEwjPtK6Fwv3PAhVGI5AKHTkSDVsQxdoBCE8" type="button" />


Any help is much appreciated.

Answer

Have you tried

driver.find_element_by_id('smb').click()

Rather than

driver.find_element_by_partial_link_text('Mais').click()

I suspect nothing happens because an exception is thrown in your original code if the find fails. When attempting to locate elements I would usually use id/name as these are normally unique. For a full list see selenium-python.readthedocs.io/locating-elements.html