У меня проблема с созданием Instagram-подобного бота с Selenium.
Я пытаюсь написать код, который передает картинку, если она понравилась, но она не работает.
Вот мой код:
def Like_photoTags_and_commnet(self,hashtag,comment):
driver=self.driver
driver.get("https://www.instagram.com/explore/tags/" + hashtag + "/")
time.sleep(2)
pic_hrefs = []
for i in range(1,3):
driver.execute_script("window.scrollTo(0,document.scrollHeight);")
time.sleep(2)
#searching for pictures link
hrefs_in_view = driver.find_elements_by_tag_name('a')
# finding relevant hrefs
hrefs_in_view = [elem.get_attribute('href') for elem in hrefs_in_view
if '.com/p/' in elem.get_attribute('href')]
# building list of unique photos
[pic_hrefs.append(href) for href in hrefs_in_view if href not in pic_hrefs]
print("Check: pic href length " + str(len(pic_hrefs)))
for pics in pic_hrefs:
driver.get(pics)
#if picture liked then continue
if driver.find_element_by_xpath("//button/span[@aria-label='UnLike']"):
continue
else:
driver.find_element_by_xpath("//button/span[@aria-label='Like']").click()
if comment in driver.page_source:
continue
else:
driver.find_element_by_class_name("Ypffh").click()
for letter in comment:
driver.find_element_by_class_name("Ypffh").send_keys(letter)
time.sleep(random.randint(1,2))
driver.find_element_by_class_name("Ypffh").send_keys(Keys.ENTER)
time.sleep(5)
Я получаю эту ошибку в консоли.
проблема в строке 52. У вас есть предложение?
Traceback (most recent call last):
File "C:/Users/behro/PycharmProjects/untitled/Test.py", line 52, in Like_photoTags_and_commnet
if driver.find_element_by_xpath("//button/span[@aria-label='UnLike']"):
File "C:\Users\behro\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\behro\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\behro\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\behro\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button/span[@aria-label='UnLike']"}
(Session info: chrome=74.0.3729.157)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17763 x86_64)