I'm using Mechanize
to interact with a few web pages, and I'm trying to determine whether a given form submission resulted in an error.
Right now I'm doing this:
agent.page.body.include?("I'm an error message!")
But I just discovered another error message. Since I don't want to do:
agent.page.body.include?("I'm an error message!") || agent.page.body.include?("Another error message")
How can I determine whether the page body contains either error message?
Using String.contains() method for each substring. You can terminate the loop on the first match of the substring, or create a utility function that returns true if the specified string contains any of the substrings from the specified list.
The String. indexOf method returns the index of the first occurrence of a substring in a string, or -1 if the substring is not contained in the string. If the indexOf method doesn't return -1 , the substring is contained in the string.
includes() You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.
Using Python's "in" operator The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .
error_messages.any? { |mes| agent.page.body.include? mes }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With