首页 > 解决方案 > 错误:来自 csv 的 Python Whatsapp 消息。汽车

问题描述

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys

import csv
import time
caps = DesiredCapabilities().CHROME

options = webdriver.ChromeOptions()

prefs = {'profile.default_content_setting_values': {'images': 2}}
options.add_experimental_option('prefs', prefs)
options.add_argument("start-maximized")
driver = webdriver.Chrome(desired_capabilities = caps,options = options)

file_name = 'Projeto3.csv'
csvfile = open(file_name,'rt')
csvReader = csv.reader(csvfile, delimiter = ",")

list_ = list()
for row in csvReader:
    list_.append((row[0],row[1]))
    length = len(list_)

counter = 0

class send_message_to:
    def __init__ (self, name, phonenumber):
        self.name = name
        self.phonenumber = phonenumber

def start():
    driver.get('https://web.whatsapp.com/')
    while True:
        try:
            driver.find_element_by_xpath('//*[@id="side"]/div[1]/div/div')
            break
        except:
            print("Please scan QR code")
            time.sleep(2)

        reciver = send_message_to(list_[counter][0],list_[counter][1])
        message = 'Hi {}, this is a important message'.format(reciver.name)

        url = 'https:api.whatsapp.com/send?phone={}'.format(reciver.phonenumber)
        driver.get(url)

        while True:

            try:
                driver.find_element_by_xpath('//*[@id="action-button"]').click()
                break
            except:
                pass
        
        while True:
            try:

                element = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
                element.click()
                element.send_keys(message)
                element.send_keys(Keys.ENTER)
                break
            except:
                pass
for i in range(0,length):
    start(counter)
    counter = +1

错误:

Traceback (most recent call last):
File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,

File "C:\Program Files (x86)\Python38-32\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files (x86)\Python38-32\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] O sistema não pode encontrar o arquivo especificado

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\victo\Desktop\Victor\Python Learning\Project 03 - Whatsapp\Projeto3.py", line 15, in <module>
driver = webdriver.Chrome(desired_capabilities = caps,options = options)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(

selenium.common.exceptions.WebDriverException:消息:“chromedriver”可执行文件需要在 PATH 中。请参阅https://sites.google.com/a/chromium.org/chromedriver/home

标签: pythonseleniumcsvgoogle-chromewebdriver

解决方案


尝试将此添加到您的代码中:

chromedriver = "/path/to/chromedriver"
options.binary_location = '/path/to/chrome'
driver = webdriver.Chrome(chromedriver, chrome_options=options)

推荐阅读