【问题标题】:Python Selenium Webdriver - Handling mouseover on InstagramPython Selenium Webdriver - 在 Instagram 上处理鼠标悬停
【发布时间】:2020-12-22 04:12:39
【问题描述】:

我正在创建 Instagram 取消关注工具。该代码通常有效,但偶尔 Instagram 会显示某些用户的信息(就像我将鼠标悬停在该用户上方一样)并导致我的代码停止运行,因为它掩盖了我需要单击才能取消关注用户的按钮。

举个例子更容易理解:

如何编辑我的代码以使鼠标悬停信息消失(有没有办法关闭这些鼠标悬停效果)?

我的代码:

for i in range(num_following):
    following = self.browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/ul/div/li[{}]/div/div[1]/div[2]/div[1]/span/a".format(i))
    following_username = following.get_attribute("title")   
    
    if following_username not in followers:
        # Unfollow account
        following_user_button = self.browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/ul/div/li[{}]/div/div[2]/button".format(i))
        following_user_button.click()

        unfollow_user_button = self.browser.find_element_by_xpath("/html/body/div[6]/div/div/div/div[3]/button[1]")
        unfollow_user_button.click()

        print("You've unfollowed {}.".format(following_username))

我得到的错误:

selenium.common.exceptions.ElementClickInterceptedException: Message: Element is not clickable at point (781,461) 因为另一个元素遮住了它

【问题讨论】:

  • 请您在问题中插入您的代码 sn-p 否则我们很难理解真正的问题。例如,为什么你的光标甚至在这个位置?发送.click() 或例如driver.execute_script("window.scrollTo(0, Y)") 与 Selenium 不应该移动你的鼠标,那么为什么光标首先在那里?
  • @J.M.Arnold 我已经添加了代码。我实际上并没有在屏幕上看到光标,但出现的弹出窗口与我将光标悬停在用户名上时出现的弹出窗口相同。通常我可以在弹出窗口出现之前取消关注多个帐户。
  • 我试过了,我的光标又不动了,所以没有弹出窗口神奇地出现?此外,即使有一个,您仍然可以单击 Following 按钮,就像在 Selenium 上使用 Google Chrome WebDriver 87.0.4280.88 尝试的那样。
  • @J.M.Arnold 我不知道为什么它对我不起作用。我收到以下错误:“selenium.common.exceptions.ElementClickInterceptedException:消息:元素在点 (781,461) 处不可点击,因为另一个元素遮住了它”。我刚刚在我的问题中添加了一个 GIF 以显示它的外观。 (运行程序时,我根本不移动光标。)

标签: python selenium webdriver instagram


【解决方案1】:

您想要在其上执行 .click() 的元素 unfollow_user_button = self.browser.find_element_by_xpath("/html/body/div[6]/div/div/div/div[3]/button[1]") 似乎被临时或永久覆盖阻止了。

在这种情况下,您可以使用ExplicitWaitExplicitConditions 等待,直到所述阻塞元素消失-尽管据我所知,这在这种特定情况下不起作用,如果什么都不做,弹出窗口仍然存在。另一种方法是使用JavascriptExecutor 将点击直接发送到元素:

#Find the element - by_xpath or alike
unfollow_user_button = driver.find_element_by_xpath("XPATH")

#Sending the click via JavascriptExecutor
driver.execute_script("arguments[0].click();", unfollow_user_button)

注意两点:

  1. driver 显然必须是 WebDriver 的一个实例。

  2. 我建议一般不要使用绝对 XPath。使用相对 XPath 不太容易被站点结构的微小变化破坏。点击here 阅读小指南。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多