简体   繁体   English

PYTHON -INSTAGRAM 硒

[英]PYTHON -INSTAGRAM Selenium

I have been stuck at the code from a long time:很长一段时间我都被代码困住了:

so i want to send dm's using python on instagram but i am having a problem slecting the user from drop down menu like the text even goes in here, but now after this how do i select the first person?所以我想在 instagram 上使用 python 发送 dm,但是我在从下拉菜单中选择用户时遇到问题,就像文本甚至进入这里一样,但是现在之后我如何选择第一个人?

I have tried using the find xpath method or get the class method or even tried using the button containing the text.我曾尝试使用 find xpath 方法或 get 类方法,甚至尝试使用包含文本的按钮。

please help me.请帮我。

from selenium import webdriver
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select


x=input("Whom to you want to send your text message? Please type his/her name:- ")
print("Okay cool ! I got it !")
y=input("What is the message? Don't worry you can send any personal messages, I don't save them in my memory xD !")

class Instabot:
    def __init__(self, username, password):
        self.driver = webdriver.Chrome()
        self.driver.get("https://instagram.com")
        sleep(2)
        self.driver.find_element_by_xpath("//input[@name=\"username\"]")\
            .send_keys(username)
        self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
            .send_keys(password)
        self.driver.find_element_by_xpath('//button[@type="submit"]').click()
        sleep(3)
        self.driver.find_element_by_xpath('//button[text()="Not Now"]').click()
        sleep(2)
        self.driver.find_element_by_xpath('//button[text()="Not Now"]').click()
        sleep(2)
        self.driver.find_element_by_xpath("//input[@placeholder=\"Search\"]")\
            .send_keys(x)
        sleep(2)
        select = Select(self.driver.find_element_by_id(x))
        select.select_by_visible_text(x)
        
        sleep(1)
        self.driver.find_element_by_xpath('//button[text()="Message"]').click()
        sleep(1)
        
        k=self.driver.find_element_by_tag_name('textarea')
        k.send_keys(y)
        sleep(1)
        self.driver.find_element_by_xpath('//button[text()="Send"]').click()
        sleep(1)
        
        
        
        
        
Instabot("username","password",)

Full working code.完整的工作代码。 This will select the first suggestion, Visit the profile page, if the message option exists, it will click on message, and send the message.这将选择第一个建议,访问个人资料页面,如果消息选项存在,它将单击消息,并发送消息。

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

x = input("Whom to you want to send your text message? Please type his/her name:- ")
print("Okay cool ! I got it !")
y = input("What is the message? Don't worry you can send any personal messages, I don't save them in my memory xD !")


class Instabot:
    def __init__(self, username, password):
        self.driver = webdriver.Chrome()
        self.driver.get("https://instagram.com")
        sleep(2)
        self.driver.find_element_by_xpath("//input[@name=\"username\"]") \
            .send_keys(username)
        self.driver.find_element_by_xpath("//input[@name=\"password\"]") \
            .send_keys(password)
        self.driver.find_element_by_xpath('//button[@type="submit"]').click()
        sleep(3)
        self.driver.find_element_by_xpath('//button[text()="Not Now"]').click()
        sleep(2)
        self.driver.find_element_by_xpath('//button[text()="Not Now"]').click()
        sleep(2)
        self.driver.find_element_by_xpath("//input[@placeholder=\"Search\"]") \
            .send_keys(x)
        sleep(3)
        self.driver.find_element_by_xpath("//*[@id=\"react-root\"]/section/nav/div[2]/div/div/div[2]/div[4]/div/a[1]/div/div[2]/div").click()
        sleep(3)


        sleep(1)
        self.driver.find_element_by_xpath('//button[text()="Message"]').click()
        sleep(4)

        k = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea')
        k.send_keys(y)
        sleep(1)
        self.driver.find_element_by_xpath('//button[text()="Send"]').click()
        sleep(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM