我正在尝试Instagram追踪/像机器人一样。 我是Python的初学者。
此代码段有效:
try:
time.sleep(15)
driver.refresh()
time.sleep(3)
if len(driver.find_elements_by_class_name('fr66n')) > 0:
print("LIKE")
time.sleep(randint(2, 3))
likeButton = driver.find_element_by_class_name('fr66n')
time.sleep(randint(2, 3))
likeButton.click()
time.sleep(randint(15, 20))
else:
print("FOLLOW")
time.sleep(randint(2, 3))
driver.find_element_by_class_name("eLAPa").click()
time.sleep(randint(2, 3))
driver.find_element_by_xpath(
'/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').click()
time.sleep(randint(15, 20))
除外: print(“错误”)
当我在for循环中尝试时,它停止工作。 错误:无法找到CSS选择器?
for i in i_element:
print(i.get_attribute("href"))
time.sleep(randint(3, 6))
i.click()
try:
time.sleep(randint(15, 20))
driver.refresh()
if len(driver.find_elements_by_class_name('fr66n')) > 0:
print("LIKE")
time.sleep(randint(2, 3))
likeButton = driver.find_element_by_class_name('fr66n')
time.sleep(randint(2, 3))
likeButton.click()
time.sleep(randint(15, 20))
else:
print("FOLLOW")
time.sleep(randint(3, 5))
driver.find_element_by_class_name("eLAPa").click()
time.sleep(randint(2, 3))
driver.find_element_by_xpath(
'/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').click()
time.sleep(randint(15, 20))
except:
print('ERROR')
Instagram()
希望您能帮助我谢谢
答案 0 :(得分:0)
您的for循环格式错误。试试这个:
for i in i_element: #moved the for loop back
print(i.get_attribute("href"))
time.sleep(randint(3, 6))
i.click()
try:
time.sleep(randint(15, 20))
driver.refresh()
if len(driver.find_elements_by_class_name('fr66n')) > 0:
print("LIKE")
time.sleep(randint(2, 3))
likeButton = driver.find_element_by_class_name('fr66n')
time.sleep(randint(2, 3))
likeButton.click()
time.sleep(randint(15, 20))
else:
print("FOLLOW")
time.sleep(randint(3, 5))
driver.find_element_by_class_name("eLAPa").click()
time.sleep(randint(2, 3))
driver.find_element_by_xpath(
'/html/body/div[4]/div[2]/div/article/header/div[2]/div[1]/div[2]/button').click()
time.sleep(randint(15, 20))
except:
print('ERROR')
Instagram()
答案 1 :(得分:0)
T
raceback (most recent call last):
File "C:/Users/Georg/PycharmProjects/Giraffe/app.py", line 87, in Instagram
driver.find_element_by_class_name("eLAPa").click()
File "C:\Users\Georg\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Users\Georg\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Georg\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Georg\PycharmProjects\Giraffe\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":"css selector","selector":".eLAPa"}
(Session info: chrome=83.0.4103.116)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Georg/PycharmProjects/Giraffe/app.py", line 129, in <module>
Instagram()
File "C:/Users/Georg/PycharmProjects/Giraffe/app.py", line 126, in Instagram
Instagram()
File "C:/Users/Georg/PycharmProjects/Giraffe/app.py", line 94, in Instagram
Instagram()
File "C:/Users/Georg/PycharmProjects/Giraffe/app.py", line 126, in Instagram
Instagram()
File "C:/Users/Georg/PycharmProjects/Giraffe/app.py", line 126, in Instagram
Instagram()
File "C:/Users/Georg/PycharmProjects/Giraffe/app.py", line 126, in Instagram
Instagram()
[Previous line repeated 8 more times]
File "C:/Users/Georg/PycharmProjects/Giraffe/app.py", line 66, in Instagram
if len(driver.find_elements_by_xpath(xpath_Instagram)) > 0:
File "C:\Users\Georg\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 410, in find_elements_by_xpath
return self.find_elements(by=By.XPATH, value=xpath)
File "C:\Users\Georg\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1005, in find_elements
return self.execute(Command.FIND_ELEMENTS, {
File "C:\Users\Georg\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Georg\PycharmProjects\Giraffe\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
(Session info: chrome=83.0.4103.116)