Advanced use of scripting

Advanced use of scripting

I am experimenting a bit with web scripting.  At the moment I am looking for a bit more advanced scripting and I would like to see how in general javascript and web scripting is integrated.
For example, many webpages today start off with a "cookie agreement popup".
I found how I can click the popup using a click("""selector""") call.  However, when the selector does not appear in the content, that just waits until it does.  Appropriate for pages that have the same content all the time, but these cookie popups only appear until they are confirmed.   So I would want to have a click() function that only clicks when that content is available, and else it just continues.  Maybe this can be solved with extra javascript?
(if not, it would be nice if e.g. the click() function could have an optional second argument that specifies a timeout and that just continues when the content is not available)

Similarly, during operation of the webpage I need to watch some CSS and conditionally click on a button.  It is a pause/play button that I only want to click when it is showing the Play symbol, not when it is showing the Pause symbol.   But this is manipulated on the webpage itself using javascript, by setting "display: none;" or "display: block;" on the item.  How would I go about to do a clock() on an item only when it has "display: block;" CSS attribute?