我正在尝试使用Python和Selenium对Instagram进行网络搜刮。我在定位元素方面遇到了很多问题,但当我尝试了足够多的xpaths后,还是设法解决了。但是,当我尝试网络抓取唐纳德-特朗普的以下列表时(我希望这对任何用户的 following
listpage),但它就是不工作。以下是它抛出的错误。
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="f3b066159b38864"]/div/div/a"}
我使用谷歌浏览器的检查功能右击元素就能得到xpaths。如果有人需要我发布完整的代码,我很乐意这样做。
试试下面的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