我一直在尝试用 python 实现一个简单的机器人,它可以增加 Instagram 的粉丝。这就是我正在尝试的-> https://towardsdatascience.com/increase-your-instagram-followers-with-a-simple-python-bot-fde048dce20d在此过程中,我按照上述帖子中的指定安装了 chromedriver,并将其路径包含在 webdriver.chrome 中。我收集了 Instagram 中登录按钮的 HTML 元素(复制选择器)并将其粘贴到 find_element_by_name 方法中,但收到一条错误消息,指出“NoSuchElementException:没有这样的元素:无法定位元素:{“method”:“name” ","selector":"#react-root > 部分 > 主 > div > 文章 > div > div:nth-child(1) > div > 表单 > div:nth-child(3) > 按钮"} ( session 信息:chrome=70.0.3538.110) (驱动程序信息:chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 10.0.17134 x86_64)"
请在下面找到我的代码并帮助我解决此错误。提前致谢
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
webdriver=webdriver.Chrome(executable_path="C:\\Users\\Vishal\\chromedriver.exe")
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)
用户名 = webdriver.find_element_by_name('#react-root > 部分 > main > div > 文章 > div > div:nth-child(1) > div > form > div:nth-child(3) >按钮')
最佳答案
添加一些等待以确保可见性并且可以单击登录。使用较短的选择器来定位按钮。请注意,您需要向前两个登录字段添加值以使按钮可单击。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
url = 'https://www.instagram.com/accounts/login/?source=auth_switcher'
d = webdriver.Chrome()
d.get(url)
WebDriverWait(d,5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[name=username]"))).send_keys('bob')
d.find_element_by_css_selector('[name=password]').send_keys('builder')
WebDriverWait(d, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form div:nth-of-type(3)"))).click()
关于python - 当我在构建机器人时尝试包含登录按钮的 HTML 详细信息时,收到 "NoSuchElementException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53656597/