python - WebElement 没有属性 w3c

标签 python python-3.x selenium selenium-webdriver action

我正在尝试在给定用户输入的情况下浏览 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

我一直收到这个错误:

packages\selenium\webdriver\common\action_chains.py", line 69, in __init__
        if self._driver.w3c:
    AttributeError: 'WebElement' object has no attribute 'w3c'

我四处搜索但没有找到任何关于 w3c 的信息。我以前从未在页面上打过标签,所以我使用在这里找到的答案:Send multiple tab key presses with selenium .

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

最佳答案

ActionChains 应该接收 WebDriver,但您发送的是 WebElement

actions = ActionChains(driver)

关于python - WebElement 没有属性 w3c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48046342/

相关文章:

javascript - 如何点击没有固定类别值的按钮?但它有一个固定的 'data-testid'

selenium - 使用 Selenium Webdriver PHP 选择元素?

selenium - 错误 : self signed certificate in certificate chain

python - pip 安装不起作用——(错误 : Could not find a version that satisfies the requirement Flask (from versions: none))

python - 编写 MySQL Python 类的最佳方法?

python - 语音识别python代码不起作用

python - 如何从列表中删除/删除每个第 n 个元素?

python - 列表理解中for语句的顺序

python - 使用多处理将 .wav 批量转换为 .flac(Python、Pydub)

python - 内置模块位于何处?