2017-12-31 202 views
0

ユーザー入力がある場合、Instagramページを介してタブしています。私はそのページに行くことができます。ページがロードされ、クラスが見つかった場合、コードが破損します。私はこのエラーを得続けるWebElementには属性がありませんw3c

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は、ページを複数回タブする最善の方法のようですが、誰かがより良い方法を持っている場合は、私はそれを試してみることができます。

答えて

1

ActionChainsWebDriverを受ける必要がありますが、あなたの代わりに

actions = ActionChains(driver) 
WebElementを送信します
関連する問題