wbot.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from selenium import webdriver
  2. from selenium.common.exceptions import NoSuchElementException
  3. from selenium.webdriver.common.keys import Keys
  4. from time import sleep
  5. browser = None
  6. def start():
  7. global browser
  8. browser = webdriver.Firefox(executable_path='/home/luca/Pojects/wbot/WhatsAppBot/geckodriver') #Pfad zum geckodriver
  9. browser.get('https://web.whatsapp.com/')
  10. print('Scan QR')
  11. def send_messages_to_contact(contact_name, *messages):
  12. while True:
  13. try:
  14. search = browser.find_element_by_css_selector('#side > div._3CPl4 > div > label > input')
  15. except NoSuchElementException:
  16. print('exception')
  17. sleep(1)
  18. continue
  19. break
  20. title = None
  21. try:
  22. title = browser.find_element_by_css_selector('#main > header > div._1WBXd > div > div > span').text
  23. except NoSuchElementException:
  24. pass
  25. if title != contact_name:
  26. search.send_keys(contact_name)
  27. search.send_keys(Keys.ENTER)
  28. try:
  29. not_found = browser.find_element_by_css_selector('#pane-side > div > div > span').text
  30. if not_found == 'No chats, contacts or messages found' or not_found == 'Es wurden keine Chats, Kontakte oder Nachrichten gefunden':
  31. return
  32. except:
  33. pass
  34. try:
  35. input = browser.find_element_by_css_selector('#main > footer > div._3oju3 > div._2bXVy > div > div._2S1VP.copyable-text.selectable-text')
  36. except NoSuchElementException:
  37. return
  38. for m in messages:
  39. for c in m:
  40. if c == '\n':
  41. input.send_keys(Keys.SHIFT, Keys.ENTER)
  42. else:
  43. input.send_keys(c)
  44. input.send_keys(Keys.ENTER)
  45. print('Message sent to ' + contact_name + '.')
  46. sleep(0.6)
  47. def retrieve_newest(contact_name, num):
  48. while True:
  49. try:
  50. #search = browser.find_element_by_id('input-chatlist-search')
  51. search = browser.find_element_by_css_selector('#side > div._3CPl4 > div > label > input')
  52. except NoSuchElementException:
  53. sleep(1)
  54. continue
  55. break
  56. title = None
  57. try:
  58. title = browser.find_element_by_css_selector('#main > header > div._1WBXd > div > div > span').text
  59. except NoSuchElementException:
  60. pass
  61. if title != contact_name:
  62. search.send_keys(contact_name)
  63. search.send_keys(Keys.ENTER)
  64. try:
  65. not_found = browser.find_element_by_css_selector('#pane-side > div > div > span').text
  66. if not_found == 'No chats, contacts or messages found' or not_found == 'Es wurden keine Chats, Kontakte oder Nachrichten gefunden':
  67. return
  68. except:
  69. pass
  70. messages = browser.find_elements_by_css_selector('div > div > div > div.copyable-text > div > span')
  71. newest = []
  72. for i in range(num):
  73. try:
  74. newest.append(messages[i].text)
  75. except IndexError:
  76. break
  77. if i == len(messages) - 1:
  78. browser.find_element_by_tag_name('html').send_keys(Keys.ARROW_UP)
  79. browser.find_element_by_tag_name('html').send_keys(Keys.ARROW_UP)
  80. messages = browser.find_elements_by_css_selector('div > div > div > div.copyable-text > div > span')
  81. return list(reversed(newest))
  82. def set_about(status):
  83. while True:
  84. try:
  85. browser.find_element_by_css_selector('#side > header > div._2umId > div > img').click()
  86. except NoSuchElementException:
  87. sleep(1)
  88. continue
  89. break
  90. sleep(1)
  91. browser.find_element_by_css_selector('#app > div > div > div.MZIyP > div._3q4NP.k1feT > span > div > div > div > div:nth-child(4) > div.ogWqZ._2-h1L > div._1DTd4._1G2k- > div._2YmC2 > span:nth-child(1) > div').click()
  92. input = browser.find_element_by_css_selector('#app > div > div > div.MZIyP > div._3q4NP.k1feT > span > div > div > div > div:nth-child(4) > div.ogWqZ._2-h1L._31WRs > div._1DTd4 > div._3F6QL.bsmJe > div._2S1VP.copyable-text.selectable-text')
  93. input.clear()
  94. input.send_keys(status)
  95. input.send_keys(Keys.ENTER)
  96. print('About changed to "' + status + '".')
  97. browser.find_element_by_css_selector('#app > div > div > div.MZIyP > div._3q4NP.k1feT > span > div > div > header > div > div.SFEHG > button').click()
  98. def close():
  99. browser.close()