H
H
HelSay2019-08-28 14:00:44
Python
HelSay, 2019-08-28 14:00:44

My parser in Python and Selenium works, but my friend does not, what's the problem?

Good afternoon! Help solve the problem. They asked me to write a script that parses numbers from WhatsApp Web. I wrote a script, using PyInstaller I created an exe file. On my computer and on my old one, where even python is not there, everything works correctly and as it should. But for some reason, a friend's script crashes. Chrome Driver is on all 3 computers.
The principle of operation is as follows:
Chrome opens with the WhatsAppWeb tab. The program waits for the user to log in and press Enter in the console that opens, then it starts parsing.
A window opens for a friend, he logs in, presses Enter and everything closes. He lives in another city, so I can’t deal with it myself, can you tell me what the problem could be?! (
Here is the code:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException, WebDriverException, ElementNotInteractableException
from time import sleep, time

timeout = 10


def click_option_btn():
  attr = ""
  x = 1
  while attr != "Menu":
    opt_btn = browser.find_element_by_xpath("//*[@id='main']/header/div[3]/div/div[%i]/div" % x)
    attr = opt_btn.get_attribute("title")
    x += 1
  opt_btn.click()


def click_userinfo_btn():
  start_time = int(time())
  while time() < start_time + timeout:
    try:
      user_info_btn = browser.find_element_by_xpath("//*[@id='main']/header/div[3]/div/div[3]/span/div/ul/li[1]")
    except NoSuchElementException:
      user_info_btn = browser.find_element_by_xpath("//*[@id='main']/header/div[3]/div/div[2]/span/div/ul/li[1]")
    try:
      user_info_btn.click()
      break
    except ElementNotInteractableException:
      print("Cannot click(")
      sleep(0.2)
    except ElementClickInterceptedException:
      print("Cannot click(")
      sleep(0.2)


def click_chat_btn():
  btns = browser.find_elements_by_class_name("_3j8Pd")
  for element in btns:
    obj = element.find_element_by_tag_name("div")
    if obj.get_attribute("title") == "New chat":
      obj.click()
      break


try:
  browser = webdriver.Chrome()

except WebDriverException:
  print("Problem with ChromeDriver")
  input("Enter some to quit.")

else:
  browser.get("https://web.whatsapp.com/")

  phone_number = "+7000000000"
  numbers = []

  # Waiting authorization
  input("Please authorize in WhatsAppWeb, wait a few second and press Enter.")
  i = 2
  # Parse numbers
  while True:
    click_chat_btn()

    try:
      person = browser.find_element_by_xpath("//*[@id='app']/div/div/div[2]/div[1]/span/div/span/div/div[2]/div[2]/div/div/div[%i]" % i)
    except NoSuchElementException:
      break

    start_time = int(time())
    while time() < start_time + timeout:
      try:
        person.click()
        break
      except ElementClickInterceptedException:
        print("Failed to click!")
        sleep(0.1)
      except ElementNotInteractableException:
        print("Failed to click!")
        sleep(0.1)

    click_option_btn()

    click_userinfo_btn()

    phone_number = ""

    start_time = int(time())
    while time() < start_time + timeout:
      try:
        # Waiting while field is empty
        while len(phone_number) <= 1:
          # Write user number phone
          phone_number = browser.find_element_by_xpath("//*[@id='app']/div/div/div[2]/div[3]/span/div/span/div/div/div[1]/div[4]/div[3]/div/div/span/span").text
        break
      except NoSuchElementException:
        print("Cannot find element")
        sleep(0.3)

    numbers.append(phone_number)
    i += 1

  print(numbers)

  # Clear useless symbols
  useless_symbols = "()+ .-"
  correct_numbers = []
  for ph_number in numbers:
    for sym in useless_symbols:
      ph_number = ph_number.replace(sym, "")
    correct_numbers.append(ph_number)

  numbers = correct_numbers
  correct_numbers = []

  # Delete same phone numbers
  for i in numbers:
    if i not in correct_numbers:
      correct_numbers.append(i)

  # Writing numbers in file
  numbers_file = open("Numbers.txt", "w")

  for num in correct_numbers:
    numbers_file.write("%s\n" % num)

  # Closing File
  numbers_file.close()
  browser.quit()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question