简体   繁体   中英

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.

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..

##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..

  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. Time function

  def timeToSleep():
     time.sleep(3)

The getPage function is the function for redirecting to my destination ie User following

  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]

  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')..

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..

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 :

//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.

If None of yours friends follow the Person who you follow, then you can use this XPATH also :

//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. [This is quite suitable for your requirement as you want to click on every unfollow link].

HTH!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM