我想从instagram关注者列表中打印关注者名称列表。但是它只会向下滚动,在控制台中不会打印出超过7个关注者名称。这是我的代码:
def another_user_followers(browser, acc_username, no_of_followers_to_follow):
signIn(browser)
search_box = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located(
(By.XPATH, "/html/body/div[1]/section/nav/div[2]/div/div/div[2]/input")
)
)
search_box.send_keys(acc_username)
time.sleep(1)
search_box.send_keys(Keys.ENTER)
time.sleep(1)
search_box.send_keys(Keys.ENTER)
time.sleep(2)
try:
browser.find_element_by_css_selector(
"#react-root > section > main > div > header > section > ul > li:nth-child(2) > a").click()
except:
print("This Account is Private or It is a Tag")
browser.find_element_by_xpath("/html/body/div[1]/section/nav/div[2]/div/div/div[1]/a/div/div").click()
sys.exit()
time.sleep(2)
browser.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/button").click()
time.sleep(2)
browser.find_element_by_css_selector(
"#react-root > section > main > div > header > section > ul > li:nth-child(2) > a").click()
for i in range(1, no_of_followers_to_follow):
followers_list = []
str1 = "/html/body/div[4]/div/div[2]/ul/div/li["
str2 = "]/div/div[3]"
final_string = str1 + str(i) + str2
a = browser.find_element_by_xpath(final_string)
if i % 6 == 0:
followedPopup = browser.find_element_by_xpath("//div[@class='isgrP']")
browser.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;',followedPopup)
time.sleep(1)
followers_list.append(a.text)
time.sleep(1)
print(followers_list)
else:
followers_list.append(a.text)
time.sleep(1)
print(followers_list)
这里是错误消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[4]/div/div[2]/ul/div/li[7]/div/div[3]"}
(会话信息:chrome = 80.0.3987.149)
我的代码有什么问题?我希望在控制台中打印100个关注者。但是只打印6个名字
与从html和while解析相比,最好指定特定的类使用find_element_by_xpath应该以2个正斜杠('//')]开始
driver.find_element_by_xpath('//html...')