WebElement 没有属性 w3c

Mws*_*cer 1 python selenium action python-3.x selenium-webdriver

我试图在给定用户输入的情况下浏览 Instagram 页面。我能够进入该页面。页面加载,然后找到类,然后代码中断。这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys 
from  bs4 import BeautifulSoup
import urllib.request, urllib.parse, urllib.error
import time

def get_posts(tag, user_count):

    '''Getting html of page to be scrape for
    hrefs that will get me the user names'''

    print("getting page")
    url = "https://www.instagram.com/explore/tags/" + tag + "/"
    try:
        driver = webdriver.Chrome()
        driver.get(url)
        print("successfully requested site")
    except:
        print("Unable to reach site")
        quit()

    browser = driver.find_element_by_class_name('_si7dy')
    actions = ActionChains(browser)
    for i in range(user_count):
        actions = actions.send_keys(Keys.TAB)
        time.sleep(0.5)
    actions.perform()



    soup = BeautifulSoup(driver.page_source, 'lxml')

    try:
        posts = soup.find_all("div", class_ = ["_mck9w","_gvoze","_f2mse"])
    except:
        print("No links found")
        quit()

    print("Length of posts: ",(len(posts)))

    print(len(posts))
    print(type(posts))
    print("All Done")
    driver.close()
    return posts
Run Code Online (Sandbox Code Playgroud)

我不断收到此错误:

packages\selenium\webdriver\common\action_chains.py", line 69, in __init__
        if self._driver.w3c:
    AttributeError: 'WebElement' object has no attribute 'w3c'
Run Code Online (Sandbox Code Playgroud)

我四处搜索,但没有找到有关 w3c 的任何信息。我以前从未使用过标签页,所以我使用这里找到的答案:使用 selenium 发送多个标签键按下

ActionChains 似乎是在页面上多次使用 Tab 的最佳方式,但如果有人有更好的方法,我愿意尝试。

Guy*_*Guy 7

ActionChains应该接受WebDriver,但你要发送WebElement,而不是

actions = ActionChains(driver)
Run Code Online (Sandbox Code Playgroud)