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. After inspecting 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" />
2 Answers 2
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
3 Comments
You are having issue clicking the button because you are using driver.find_element_by_partial_link_text()
which is only good for finding link (anchor elements) by its partial text, and performing actions on it.
You should rely on the other find_element_by_xxxx()
methods which allow us to find by element ID, Name, XPath, CSS selector, or Class name.
Refer the documentation on locating elements at : http://selenium-python.readthedocs.io/locating-elements.html
Comments
Explore related questions
See similar questions with these tags.