[英]selenium.common.exceptions.NoSuchElementException: Message: no such element error invoking click on followers link within Instagram with Selenium
I am building an Instagram bot using selenium, and I want to click on my followers to open the complete list of followers.我正在使用 selenium 构建一个 Instagram 机器人,我想单击我的关注者以打开关注者的完整列表。 The problem is, I always get the following error message:
问题是,我总是收到以下错误消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
I tried locating the element by its Xpath, class name and CSS selector but neither of those worked.我尝试通过 Xpath、类名和 CSS 选择器定位元素,但这些都不起作用。 I found a post that recommended the following method, which also did not work:
我找到了一篇推荐以下方法的帖子,但它也不起作用:
driver.find_element_by_partial_link_text("followers").click()
The strangest part is, once the DevTools are open in the same browser, all of the methods I used worked, ie opened my list of followers.最奇怪的是,一旦 DevTools 在同一个浏览器中打开,我使用的所有方法都有效,即打开我的关注者列表。 I am using Microsoft Edge Version 85.0.564.51 btw.
顺便说一句,我正在使用 Microsoft Edge 版本 85.0.564.51。
What am I missing here?我在这里缺少什么? If needed, I can provide more code.
如果需要,我可以提供更多代码。
The followers element in HTML code is an hyperlink tag : HTML 代码中的 follower 元素是一个超链接标签:
<a class="-nal3 " href="/[PROFILE_ID]/followers/" tabindex="0"><span class="g47SY " title="237">237</span> followers</a>
You can actually go to this URL :您实际上可以访问此 URL:
driver.get("https://www.instagram.com/[PROFILE_ID]/followers/")
Your method should work with this xpath instead您的方法应该与此 xpath 一起使用
driver.find_element_by_xpath("//a[contains(.,'followers')]").click()
On instagram the link of followers is as follows:在instagram上关注者的链接如下:
<a class="-nal3 " href="/PCy/followers/" tabindex="0">
<span class="g47SY " title="999">999</span>
" followers"
</a>
To click on the element you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies :要单击元素,您需要为
element_to_be_clickable()
引入WebDriverWait ,您可以使用以下任一定位器策略:
Using CSS_SELECTOR
:使用
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href$='/followers/'] > span"))).click()
Using XPATH
:使用
XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'followers')]"))).click()
Note : You have to add the following imports :注意:您必须添加以下导入:
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.