The evolution of these features has created an environment in which herbivores have then been selected to be able to eat plants despite these deterents. Some prey species have evolved toxins that deter predators. Even a toxic animal could be harmed if a predator attacked it, but if a predator can be warned away so it will not attack at all, the animal will survive better. That is the function of aposematic coloration. We see bright coloration in many poisonous and venomous species including many butterflies, such as monarchs and swallowtails, frogs, such as the poison-dart frogs of Central America and snakes, such as coral snakes.
Predators presumably avoid such species because they look like the poisonous ones.
The examples of moths flashing their wings to expose eyespots and of snakes shaking their tails are examples in which the adaptation is behavioral. These behavioral characteristics have presumably evolved through natural selection, just as biochemical and structural characteristics have. Species interact with each other in ways other than feeding upon one another. Mutualists such as these may also evolve traits in response to one another. Species also interact by competing with each other for resources.