【问题标题】:Why scroll on modal/popup doesn't working on instagram为什么在模式/弹出窗口上滚动在 Instagram 上不起作用
【发布时间】:2021-05-13 19:38:47
【问题描述】:

我很尴尬,因为我没有在 instagram 的模式上滚动。而不是它滚动背面的页面:(

我已经尝试过browser.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", modalDiv) ...我尝试过我创建的任何东西,但仍然无法正常工作。

我的代码:

browser.get('https://www.instagram.com/%s' % account)
sleep(2)

clickToFollowing= browser.find_element_by_xpath("/html/body/div[1]/section/main/div/header/section/ul/li[3]/a/span")
clickToFollowing.click()

actionChain = webdriver.ActionChains(browser)
time.sleep(2)

modalDiv= browser.find_elements_by_xpath("//html/body/div[5]/div/div/div[2]/div/div/div[4]/div[2]/div[2]")
browser.execute_script("window.scrollBy(0, 660);")

【问题讨论】:

  • 查看 iframe
  • @cruisepandey 我怎么能做到这一点??
  • 您不必制作它们,只是您必须在开发人员工具 (F12) 中确定元素是否在 iframe 内,如果是,则必须切换到该框架以便进行交互

标签: python selenium scroll instagram


【解决方案1】:

您需要在关注者列表部分滚动多次才能获取所需的一切。

代码:

options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
driver = webdriver.Chrome("C:\\Users\\etc\\Desktop\\Selenium+Python\\chromedriver.exe", options=options)
wait = WebDriverWait(driver, 30)
driver.get("https://www.instagram.com")
wait.until(EC.element_to_be_clickable((By.NAME,"username"))).send_keys('your username')
wait.until(EC.element_to_be_clickable((By.NAME,"password"))).send_keys('your password')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[type='submit']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Not Now']"))).click()
sleep(3)
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Not Now']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[name()='svg' and @aria-label='Activity Feed']/../../following-sibling::div/span"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Profile']"))).click()
sleep(3)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Change Profile Photo']/../../../following-sibling::section/descendant::li[3]/a"))).click()
sleep(3)
fBody  = driver.find_element_by_xpath("//div[@class='isgrP']")
scroll = 0
while scroll < 5: # scroll 5 times
    driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
    sleep(2)
    scroll += 1

fList  = driver.find_elements_by_xpath("//div[@class='isgrP']//li")
print("fList len is {}".format(len(fList)))
print("ended")

【讨论】:

    猜你喜欢
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2011-11-26
    • 2012-07-16
    • 1970-01-01
    相关资源
    最近更新 更多