簡體   English   中英

Selenium find_element_by_xpath 不適用於 instagram

[英]Selenium find_element_by_xpath not working for instagram

我正在嘗試 web 使用 Python 和 Selenium 抓取 Instagram。 我在定位元素方面遇到了很多問題,但是當我嘗試了足夠多的 xpath 時,我設法解決了問題。 但是,當我嘗試 web 抓取唐納德特朗普的以下列表時(我希望這適用於任何用戶的following列表/頁面),它只是不起作用。 這是它拋出的錯誤:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="f3b066159b38864"]/div/div/a"}

我通過使用 Google Chrome 的檢查功能右鍵單擊元素來獲取 xpath。 如果有人需要我發布完整的代碼,我很樂意這樣做。

試試下面 xpath::

wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'laraleatrump')]")))

注意:請將以下導入添加到您的解決方案中

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

工作解決方案:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver.get("https://www.instagram.com/realdonaldtrump/")
driver.maximize_window()
wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'following')]"))).click()

peoples =  wait.until(
            EC.visibility_of_all_elements_located((By.XPATH, "//div[@role='dialog']//div[contains(@class,'PZuss')]//a")))

        for peoplename in peoples:
            print peoplename.text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM