![](/img/trans.png)
[英]"Error: selenium.common.exceptions.NoSuchElementException: Message
[英]selenium.common.exceptions.NoSuchElementException: Message: no such element error invoking click on followers link within Instagram with Selenium
我正在使用 selenium 構建一個 Instagram 機器人,我想單擊我的關注者以打開關注者的完整列表。 問題是,我總是收到以下錯誤消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
我嘗試通過 Xpath、類名和 CSS 選擇器定位元素,但這些都不起作用。 我找到了一篇推薦以下方法的帖子,但它也不起作用:
driver.find_element_by_partial_link_text("followers").click()
最奇怪的是,一旦 DevTools 在同一個瀏覽器中打開,我使用的所有方法都有效,即打開我的關注者列表。 順便說一句,我正在使用 Microsoft Edge 版本 85.0.564.51。
我在這里缺少什么? 如果需要,我可以提供更多代碼。
HTML 代碼中的 follower 元素是一個超鏈接標簽:
<a class="-nal3 " href="/[PROFILE_ID]/followers/" tabindex="0"><span class="g47SY " title="237">237</span> followers</a>
您實際上可以訪問此 URL:
driver.get("https://www.instagram.com/[PROFILE_ID]/followers/")
您的方法應該與此 xpath 一起使用
driver.find_element_by_xpath("//a[contains(.,'followers')]").click()
在instagram上關注者的鏈接如下:
<a class="-nal3 " href="/PCy/followers/" tabindex="0">
<span class="g47SY " title="999">999</span>
" followers"
</a>
要單擊元素,您需要為element_to_be_clickable()
引入WebDriverWait ,您可以使用以下任一定位器策略:
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href$='/followers/'] > span"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'followers')]"))).click()
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.