简体   繁体   English

如何使用Selenium和python单击网页上的所有“ role = button”

[英]How to Click on all “role=button” on a webpage using selenium and python

I'm trying to create my first project using python and Selenium, and i'm new into it, it's just a simple project that ask user for his/her facebook username and password, then login redirect to the facebook.com/user/following page and unfollow everybody the user is following. 我正在尝试使用python和Selenium创建我的第一个项目,但是我是一个新手,它只是一个简单的项目,要求用户提供他/她的Facebook用户名和密码,然后登录重定向到facebook.com/user/下一页并取消关注用户关注的所有人。

Here's my code bellow 这是我的代码

I imported the necessary modules using this codes 我使用此代码导入了必要的模块

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium
import time

I closed the Facebook popup Notification to avoid disruption of the code.. 我关闭了Facebook弹出通知,以避免破坏代码。

##SHUTTING POPUP NOTIFICATION
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(chrome_options=chrome_options)

Defined a Login func that takes username and pass word after opening facebook.. 定义了一个登录func ,该func在打开Facebook后需要用户名和密码。

  def login():
        #Login
        user = input('Whats your username or email    ')
        passw = input('Input your password    ')
        driver.get("https://www.facebook.com/")
        username = driver.find_element_by_name("email")
        username.send_keys(user)
        password = driver.find_element_by_name("pass")
        password.send_keys(passw)
        password.send_keys(Keys.RETURN)

Next I just created a 3 sec. 接下来,我只创建了3秒。 Time function 时间功能

  def timeToSleep():
     time.sleep(3)

The getPage function is the function for redirecting to my destination ie User following getPage函数是用于重定向到我的目的地(即用户关注)的函数

  def getPage():
        ##Get Page
        driver.get("https://www.facebook.com/Username/ID/following")

This is the click function to click on the places i want and this is where my problem is.. I will like to start clicking on all following with the click funcion bellow.. [Check img] 这是单击功能,可以单击我想要的位置,这也是我的问题所在。.我想开始单击以下所有带有单击功能的波纹管。[检查img]

  def click():
        xpath = "//a[@role='button']"
        a=driver.find_elements_by_xpath(xpath)
        for posts in a:
              posts.click()

CLICK ON FOLLOWING 点击以下

But my problem now is this (The following button doesn't have any class or id except for an anchor attribute "role='button').. 但是我现在的问题是(除了锚属性“ role ='button'”之外,以下按钮没有任何类或ID)。

The code would have worked fine but they are other elements on the webpage with same role='button' attribute, like the pix below: Toolbar The facebook toolbars have same role button stuff.. My question is this : How do i make Selenium skip the toolbar and start clicking from the Following buttons.. 该代码可以正常工作,但它们是网页上具有相同role ='button'属性的其他元素,例如下面的pix: 工具栏 facebook工具栏具有相同的角色按钮内容。我的问题是:我如何使Selenium跳过工具栏,然后从以下按钮开始单击。

So Sorry if my explanation is bad... I'll gladly appreciate any help from any body.. 很抱歉,如果我的解释不好...我将非常感谢任何机构的帮助。

Thanks in Advance 提前致谢

I follow Mark Zuckerberg on Facebook and here is the xpath to click on unfollow link : 我在Facebook上关注Mark Zuckerberg,这是单击取消关注链接的xpath:

//a[text()='Mark Zuckerberg']/../../following-sibling::div[2]/a  

Similarly, you can replace the name of Person in your case to click on Unfollow link. 同样,您可以在情况下替换Person的名称,以单击Unfollow链接。

If None of yours friends follow the Person who you follow, then you can use this XPATH also : 如果您的朋友中没有一个跟随您关注的人,那么您也可以使用此XPATH:

//a[contains(@data-hovercard,'/ajax/hovercard/user.php?')]/../../following-sibling::div/a   

Which is independent of the Person Name. 独立于人名。 Note that this will give you list of web elements not a single element. 请注意,这将为您提供Web元素列表,而不是单个元素。 [This is quite suitable for your requirement as you want to click on every unfollow link]. [这很适合您的要求,因为您想单击每个取消关注链接]。

HTH! HTH!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python硒单击网页上的Javascript按钮 - How do I click a Javascript button on a webpage using python selenium 在 Python 中使用 Selenium 单击网页中的所有按钮 - Using Selenium in Python to click all buttons in a webpage 如何使用硒和python单击网页的隐藏按钮(如facebook组发布按钮) - how to click in a webpage hidden button like facebook groups post button using selenium and python 如何单击网页上的按钮并在使用 python selenium 单击按钮后遍历内容 - How to click on a button on a webpage and iterate through contents after clicking on button using python selenium 如何在没有文本的情况下单击带有角色按钮的 div? 使用 Python Selenium - How i can click on a div with role button without text? Using Python Selenium Selenium无法点击网页python的按钮 - Selenium can't click the button of a webpage python Python:登录网页并单击没有Selenium的按钮 - Python: Login to webpage and click button without Selenium 如何使用 python 单击位于 selenium 下的无线电输入角色 - How to click an input role as radio located under in selenium using python 如何使用硒和python单击网页上的元素 - How can i click the element on webpage using selenium with python 如何使用 Selenium 和 Python 在网页上单击 Continue Checkout gif 元素? - How to click on Continue Checkout gif element on webpage using Selenium and Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM