我正在尝试创建一个自动instagram机器人,并在尝试选择并单击按钮时遇到问题(该按钮为“故事”按钮):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class InstaBot:
def __init__(self, username, pw):
self.driver = webdriver.Chrome("C:/Users/chong/AppData/Local/Programs/Python/Python38-32/Scripts/chromedriver.exe")
self.driver.get("https://instagram.com")
self.base_url = 'https://www.instagram.com'
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.NAME, "username")))
element.send_keys("mysuername")
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.NAME, "password")))
element.send_keys("mypassword")
self.driver.find_elements_by_xpath("//div[contains(text(), 'Log In')]")[0].click()
time.sleep(2)
element = self.driver.find_element_by_class_name("cmbtv")
element.click()
time.sleep(2)
self.driver.find_elements_by_xpath("//button[contains(text(), 'Not Now')]")[0].click()
time.sleep(2)
self.driver.get('{}/{}'.format(self.base_url, 'fatcatharvey'))
element = self.driver.find_element_by_class_name('tUtVM')
element.click()
time.sleep(5)
InstaBot('randomusername', 'randompassword')
并且html代码(与所述按钮有关的代码)的一部分是这样的:
<li class="Ckrof" tabindex="-1" style="transform: translateX(24px);">
<div class=" Igw0E rBNOH YBx95 _4EzTm " style="width: 125px;">
<div class="_3D7yK" aria-disabled="false" role="menuitem" tabindex="0">
<div aria-label="Open Stories" class="aoVrC D1yaK" aria-disabled="true" role="button" tabindex="-1">
<canvas height="109" width="109" style="position: absolute; top: -5px; left: -5px; width: 87px; height: 87px;">
</canvas><div class="tUtVM" style="width: 77px; height: 77px;"><img alt="Harvey ll's profile picture" class="NCYx-" src="https://scontent-lhr8-1.cdninstagram.com/v/t51.12442-15/e35/c0.437.1125.1125a/s150x150/89402896_882451342191220_7293232057833411878_n.jpg?_nc_ht=scontent-lhr8-1.cdninstagram.com&_nc_cat=104&_nc_ohc=fRWOPUxfMBoAX8E3UGz&_nc_tp=16&oh=b1e61b04306f65b96b55c7fe9992baff&oe=5F63BF3F"></div></div><div class="eXle2">Harvey ll</div></div></div></li>
导致错误的部分是这样的:
element = self.driver.find_element_by_class_name('tUtVM')
element.click()
我得到的错误是:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".tUtVM"}
我尝试使用不同的类名,但是没有一个起作用!
我该如何解决?
答案 0 :(得分:0)
element = self.driver.find_element_by_xpath('// div [contains(@ aria-label,“ Open Stories”)] / following :: div') element.click()
答案 1 :(得分:0)
存在多个具有类名tUtVM
的元素。您可以按照以下方法解决它。
self.driver.get('{}/{}'.format(self.base_url, 'fatcatharvey'))
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(By.XPATH, '//div[@role="presentation"]'))
elements = self.driver.find_elements_by_class_name('tUtVM')
for i in range(len(elements)):
elementsTemp = self.driver.find_elements_by_class_name('tUtVM')
elementsTemp.__getitem__(i).click()
#Compete the task with opened url
self.driver.back()