I use Regex to retrieve certain content from a search box on a webpage with selenium.webDriver
.
JavaScript
x
3
1
searchbox = driver.find_element_by_class_name("searchbox")
2
searchbox_result = re.match(r"^.*(?=(())", searchbox).group()
3
The code works as long as the search box returns results that match the Regex. But if the search box replies with the string "No results"
I get error:
AttributeError: ‘NoneType’ object has no attribute ‘group’
How can I make the script handle the "No results"
situation?
Advertisement
Answer
I managed to figure out this solution: omit group()
for the situation where the searchbox reply is "No results"
and thus doesn’t match the Regex.
JavaScript
1
5
1
try:
2
searchbox_result = re.match("^.*(?=(())", searchbox).group()
3
except AttributeError:
4
searchbox_result = re.match("^.*(?=(())", searchbox)
5