【问题标题】:Instagram Following Selenium PythonInstagram 关注 Selenium Python
【发布时间】:2021-09-21 07:47:02
【问题描述】:

我想弄清楚如何让 Selenium 点击 Instagram 帐户的以下部分,这是它的 html。

<a class=" _81NM2" href="/peacocktv/following/" tabindex="0"><span class="g47SY lOXF2">219</span> following</a>

使用 instagram 时,直接的 /username/following 链接实际上并没有显示以下列表,您必须在网站上手动单击该字段才能显示。

log_in = WebDriverWait(driver, 8).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type = 'submit']"))).click()


not_now = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]')))
not_now.click()


'''not_now2 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]')))
not_now2.click()'''


search_select = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder = 'Search']")))
search_select.clear()


'''search_select.send_keys("russwest44", Keys.ENTER)
search_select.send_keys(Keys.ENTER)'''

# Clicks who the person is following:
following = driver.find_elements_by_xpath("//button[contains(text(),'following')]")
following.click()

想出解决方案的最佳方法是什么

【问题讨论】:

  • 看看instapy。它有许多预构建功能,只是让生活更简单。也非常值得看看他们组织良好的documentation

标签: python selenium instagram


【解决方案1】:

您当前的代码似乎有几个问题:

# Clicks who the person is following:
following = driver.find_elements_by_xpath("//button[contains(text(),'following')]")
following.click()

如果你注意了,你已经用标签共享了 HTML,所以你的 xpath 应该是

//a[contains(text(),'following')]

不是

//button[contains(text(),'following')]

另外,您使用的是find_elements_by_xpath,它会返回一个网络元素列表。不能直接触发.click就可以了。

要么切换到find_element 要么

following[0].click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多