找不到Class元素Selenium



我试图创建一个自动的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&amp;_nc_cat=104&amp;_nc_ohc=fRWOPUxfMBoAX8E3UGz&amp;_nc_tp=16&amp;oh=b1e61b04306f65b96b55c7fe9992baff&amp;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"}

我试过使用不同的类名,但都不起作用!

我该如何解决这个问题?

element=self.driver.find_element_by_xpathelement.click((

存在多个类名为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()

最新更新