如何在 python 中使用 selenium webdriver 打印我所有的 facebook 组?

how to print all my facebook group using selenium webdriver in python?

    # extract all groups where I am added
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    # from selenium.webdriver import ActionChains
    
    PATH = "C://Program Files (x86)//Chrome Driver//chromedriver.exe"
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--disable-notifications")
    driver = webdriver.Chrome(executable_path=PATH, options=chrome_options)
    
    driver.maximize_window()
    driver.get("https://www.facebook.com")
    
    driver.find_element_by_xpath("//input[@id='email']").send_keys("***************")
    driver.find_element_by_xpath("//input[@id='pass']").send_keys("***************")
    driver.find_element_by_xpath("//button[@name='login']").click()
    driver.implicitly_wait(5)
    
    driver.find_element_by_xpath("//*//a[@href='https://www.facebook.com/groups/?ref=bookmarks']").click()
    driver.implicitly_wait(5)
    element = driver.find_element_by_xpath("//*//div[2]//div[@class='j83agx80 cbu4d94t buofh1pr l9j0dhe7']")
    # driver.execute_script("return arguments[0].scrollIntoView(true);", element)
    driver.implicitly_wait(5)
    ActionChains(driver).move_to_element(element).perform()
    driver.implicitly_wait(60)
    
    groups = driver.find_elements_by_xpath("//*//span[@class='d2edcug0 hpfvmrgz qv66sw1b c1et5uql lr9zc1uh jq4qci2q a3bd9o3v lrazzd5p oo9gr5id']")
    driver.implicitly_wait(5)
    
    print("len = ", end='')
    print(len(groups))
    
    for i in range(len(groups)):
        print(groups[i].text)

我尝试使用下面的 xpath 仅打印组名。检查这是否也适合你。

//div[@role='separator']/following-sibling::div/following-sibling::div/a/div/div[2]/div/div/div/div[1]/span/span/span

尝试这样的操作以滚动查看群组名称。

from selenium import webdriver
import time


driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(30)

driver.get("https://www.facebook.com/")
time.sleep(40) # To login and click on Groups.
i=0
try:
    while True:
        groups = driver.find_elements_by_xpath("//div[@role='separator']/following-sibling::div/following-sibling::div/a/div/div[2]/div/div/div/div[1]/span/span/span")
        driver.execute_script("arguments[0].scrollIntoView(true);",groups[i])
        print(groups[i].text)
        i+=1
except Exception as e:
    print(e)
print(i) # Gives the count of groups
    # extract all groups where I am added
    from selenium import webdriver
    import time
    
    PATH = "C://Program Files (x86)//Chrome Driver//chromedriver.exe"
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--disable-notifications")
    driver = webdriver.Chrome(executable_path=PATH, options=chrome_options)
    
    driver.maximize_window()
    time.sleep(2)
    
    # go facebook.com
    driver.get("https://www.facebook.com/")
    time.sleep(5)
    
    # login
    driver.find_element_by_xpath("//input[@id='email']").send_keys("**********")
    driver.find_element_by_xpath("//input[@id='pass']").send_keys("**********")
    driver.find_element_by_xpath("//button[@name='login']").click()
    time.sleep(10)
    
    # loading groups page
    driver.find_element_by_xpath("//*//a[@href='https://www.facebook.com/groups/?ref=bookmarks']").click()
    time.sleep(30)
    
    # getting groups names one by one
    i = 0
    try:
        while True:
            time.sleep(0.2) # depends on internet speed
            groups = driver.find_elements_by_xpath("//div[@role='separator']/following-sibling::div/following-sibling::div/a/div/div[2]/div/div/div/div[1]/span/span/span")
            driver.execute_script("arguments[0].scrollIntoView(true);", groups[i])
            print(groups[i].text)
            i += 1
    except Exception as e:
        print(e)
    
    print(i) # Gives the count of groups