所以我试图获得一个帖子评论的名单,但阵列回来了空?我想得到喜欢这个帖子的人的名单,但我仍然得到相同的结果。我试过使用类名而没有。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
username ="________"
password ="_______"
search = "_______"
#getting webdriver path
chrome_path =r"C:\Users\dr_m_\Desktop\chromedriver.exe"
#opening google chrome
driver = webdriver.Chrome(chrome_path)
#going to instagram
driver.get("https://www.instagram.com/accounts/login/?hl=en")
#entering the username
Users=driver.find_element_by_name('username')
Users.send_keys(username)
time.sleep(2)
#entering password
pas=driver.find_element_by_name('password')
pas.send_keys(password)
#clicking the login button
driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/span""").click()
time.sleep(5)
#go to account
driver.get("https://www.instagram.com/"+search+"/")
time.sleep(10)
#open first pic
driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/div/div[2]/article/div[1]/div/div[1]/div[1]/a/div""").click()
time.sleep(5)
#getting the XPATH of the comments
c_name=driver.find_elements_by_xpath("""//*[@id="react-root"]/section/main/div/div/article/div[2]/div[1]/ul/li[2]/div/div/div/a""")
#print name
for x in range(0,len(c_name)):
print("name:")
print(c_name[x])
我试过其他方法,但仍然得到相同的结果。它不会进入循环。
注意 - 如果使用条件,因为在某些情况下li标签不包含评论者名称
使用xpath
all_comments=driver.find_elements_by_xpath("//ul/li")
total_comment_count = len(total_comment_count)
for x in range(1,total_comment_visible):
if driver.find_element_by_xpath("//ul//li["+x+"]/div/div/div/a"):
print driver.find_element_by_xpath("//ul//li["+x+"]/div/div/div/a").get_attribute("title")
使用cssSelector
c_name=driver.find_elements_by_css_selector("a.FPmhX.notranslate.TlrDj")
#print name
for x in range(0,len(c_name)):
print("name:")
print(c_name[x])