Warm tip: This article is reproduced from serverfault.com, please click

How to click on this HTML element in Python(Selenium)

发布于 2020-11-30 09:37:23

Recently, I used selenium to crawl some information on the website. And What I want to do is just clicking 'previous month button'.

So I push 'F12' button on Chrome And find HTML Code like this enter image description here

So I write code like this

webDriver.find_element_by_css_selector('span.ui-icon ui-icon-circle-triangle-w').click()

But it doesn't work, I changed multiple times changing the code like this

webDriver.find_element_by_css_selector('a.ui-icon ui-icon-circle-triangle-w').click()
webDriver.find_element_by_css_selector('ui-icon ui-icon-circle-triangle-w').click()
webDriver.find_element_by_class_name('ui-icon ui-icon-circle-triangle-w').click()

Any of those don't work. When I tried to use first one of three trial(that is : webDriver.find_element_by_css_selector('a.ui-icon ui-icon-circle-triangle-w').click()), the error pops up like this

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"a.ui-icon ui-icon-circle-triangle-w"}
 (Session info: chrome=86.0.4240.198)

I don't know why it doesn't work..

Please help

Questioner
user13232877
Viewed
0
kmpatel100 2020-11-30 23:37:46

try using,

webDriver.find_element_by_class_name("ui-icon.ui-icon-circle-triangle-w").click()

whenever you see space in HTML code you should put '.' instead of space while writing with selenium. Also, instead of writing class name just copy paste and replace space with '.' That is lot easier and removes tying errors.