Home > Back-end >  I want to get the text in the xpath only when it is a certain number
I want to get the text in the xpath only when it is a certain number

Time:01-04

I made a script that tells the earliest date on a visa site and I need help. I get the text via xpath and it sends it to me as an e-mail. I'm getting a lot of emails because it's so far away. Now, for example, it tells me the date of 13 May 2023, but I only want it to send an e-mail when it writes the date 2022. how can I do that??

element = site.find_element_by_xpath("/html/body/div[4]/main/div[4]/div[2]/table/tbody/tr[2]")
print(element.text)
sleep(5)

email_from = '[email protected]'
password = '[email protected]'
email_to = '[email protected]'

emailstring = element.text

context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:

 server.login(email_from, password)
 server.sendmail(email_from, email_to, emailstring)

sleep(60)
site.refresh()



CodePudding user response:

Check to see if the string 2022 is present in the element text. If so, then send email, otherwise do nothing.

element = site.find_element_by_xpath(...)
if "2022" in element.text:
    # send email
  • Related