python 需要帮助重写代码以适用于最新的selenium版本,(弃用警告:executable_path)

7eumitmz  于 2023-05-21  发布在  Python
关注(0)|答案(2)|浏览(189)

我试图设置一个Instagram的后续机器人,我一直得到同样的错误。我已经弄清楚了为什么它被触发,但我需要帮助将其实现到我的代码中。我相信这个错误的发生是因为这个命令在selenium 4.0版本之后就贬值了。我需要帮助重写这行:browser = webdriver.Chrome(options = options, executable_path='chromedriver.exe')与seleniums当前版本一起使用。

错误:DeprecationWarning: executable_path has been deprecated, please pass in a Service object browser = webdriver.Chrome(options = options, executable_path='chromedriver.exe') Message: invalid argument: invalid locator (Session info: chrome=111.0.5563.65)
验证码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time, random
from threading import Thread

# Profile you need to follow:
target_profile = 'target'

# Time between follows:
delay = 10

def auth(browser, username, password):
    try:
        browser.get('https://instagram.com')
        time.sleep(random.randrange(2,4))

        input_username = browser.find_element("username")
        input_password = browser.find_element("password")

        input_username.send_keys(username)
        time.sleep(random.randrange(1,2))
        input_password.send_keys(password)
        time.sleep(random.randrange(1,2))
        input_password.send_keys(Keys.ENTER)

    except Exception as err:
        print(err)
        browser.quit()

def follow(username, password, target):
    options = webdriver.ChromeOptions() 
    options.add_experimental_option("excludeSwitches", ["enable-logging"])

    browser = webdriver.Chrome(options = options, executable_path='chromedriver.exe')
    browser.set_window_size(350, 900)
    auth(browser, username, password)
    try:
        time.sleep(random.randrange(8,10))
        browser.get('https://instagram.com/' + target)
        time.sleep(random.randrange(3,6))
        try:
            browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section').find_element_by_xpath("//*[text()='Message']")
            print(f'[{username}] already follow "{target}"')
            time.sleep(random.randrange(2,4))
            browser.quit()
        except:
            browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section').find_element_by_xpath("//*[text()='Follow']").click()
            time.sleep(random.randrange(3,6))
            browser.get('https://instagram.com/' + target)
            time.sleep(random.randrange(3,6))
            try:
                browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section').find_element_by_xpath("//*[text()='Message']")
                print(f'[{username}] successfully follow "{target}"')
                time.sleep(random.randrange(2,4))
                browser.quit()
            except:
                print(f'[{username}] didnt follow "{target}"')
                time.sleep(random.randrange(2,4))
                browser.quit()
    except Exception as err:
        print(err)
        browser.quit()


f = open('bots.txt', 'r')

for line in f:
    log = line.split(':')
    follow(log[0], log[1], target_profile)
    time.sleep(delay)

我尝试在selenium版本4之后执行一个贬值的命令。

xyhw6mcr

xyhw6mcr1#

browser = webdriver.Chrome(options = option)

只删除可执行路径参数。selenium将自动检测chromedriver从同一目录.

4uqofj5v

4uqofj5v2#

如果您使用的是selenium v4.6.0或更高版本,则不需要设置driver.exe路径。selenium中的一个新工具Selenium Manager将处理浏览器和浏览器驱动程序。
您可以简单地替换下面的行:

browser = webdriver.Chrome(options = options, executable_path='chromedriver.exe')

收件人:

browser = webdriver.Chrome(options=options)

它应该工作,你应该能够摆脱这个过时的错误。
参考:Selenium Manager简介

**更新:**更改以下行:

browser.find_element_by_xpath

收件人:

browser.find_element(By.XPATH, "<your xpath>")

所需进口:

from selenium.webdriver.common.by import By

相关问题