繁体   English   中英

无法定位元素:{“method”:“css selector”,“selector”:“.Mr508”}

[英]Unable to locate element: {“method”:“css selector”,“selector”:“.Mr508”}

我正在尝试从 Instagram 中获取评论用户名和他们的评论。 但是我在用户名和 user_comment 中都只得到用户名。

我的代码如下:

try:
    load_more_comment = driver.find_element_by_xpath("//*[contains(@class,'dCJp8')]")
    print("Found {}".format(str(load_more_comment)))
    i = 0
    while load_more_comment.is_displayed() and i < 5:
        load_more_comment.click()
        time.sleep(1.5)
        load_more_comment = driver.find_element_by_xpath("//*[contains(@class,'dCJp8')]")
        print("Found {}".format(str(load_more_comment)))
        i += 1
except Exception as e:
    print(e)
    pass

user_names = []
user_comments = []
comment = driver.find_elements_by_class_name('gElp9 ')
for c in comment:
    container = c.find_element_by_class_name('Mr508')
    name = container.find_element_by_class_name('_6lAjh').text
    content = container.find_element_by_tag_name('span').text
    content = content.replace('\n', ' ').strip().rstrip()
    user_names.append(name)
    user_comments.append(content)

print(user_names)
print(user_comments)

在此处输入图片说明

请在下面找到 html 文本:

<div class="C4VMK">
    <h3 class="_6lAjh ">
        <div
            class="                     Igw0E     IwRSH      eGOV_         _4EzTm                                       ItkAi                                                                       ">
            <span class="Jv7Aj mArmR MqpiF  "><a class="sqdOP yWX7d     _8A5w5   ZIAjV " href="/_anubhavgarg/"
                    tabindex="0">_anubhavgarg</a></span></div>
    </h3><span class="">Dada for all of us and dadi for the legend.👏👏</span>
    <div
        class="                     Igw0E     IwRSH      eGOV_         _4EzTm   pjcA_                                                         aGBdT                                                  ">
        <div class="_7UhW9  PIoXz       MMzan   _0PwGv         uL8Hv         "><a class="gU-I7"
                href="/p/CRDqn-YMocE/c/17922998680684965/" tabindex="0"><time class="FH9sR Nzb55"
                    datetime="2021-07-08T07:10:38.000Z" title="Jul 8, 2021">20h</time></a><button class="FH9sR">365
                likes</button><button class="FH9sR">Reply</button></div>
    </div>
</div>

您可以尝试使用以下xpath从 Instagram 获取user comment

//button[text()='Reply']/../../preceding-sibling::span

说明:如果您在用户评论部分看到,您会在每条评论中看到Reply按钮,因此将其保留在 xpath 中使其更可靠。

示例代码:

try:
    for comments in driver.find_elements(By.XPATH, "//button[text()='Reply']/../../preceding-sibling::span"):
        print(comments.get_attribute('innerHTML'))
except:
    print("There an issue, please check the code again.")
    pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法找到硒中的元素:{“方法”:“ css选择器”,“选择器”:“ [id =” identifierId”]”} 没有这样的元素:无法定位元素:{“method”:“css selector”,“selector”:“[id=”guided-tour-tag-credentials-new“]”} NoSuchElementException:消息:没有这样的元素:无法找到元素:{&quot;method&quot;:&quot;css selector&quot;,&quot;selector&quot;:&quot;[id=&quot;events_table&quot;]&quot;} 收到消息:没有这样的元素:无法找到元素:{&quot;method&quot;:&quot;css selector&quot;,&quot;selector&quot;:&quot;[id=&quot;None&quot;]&quot;} Python 使用 selenium 时 selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“.ui流感~”} 消息:没有这样的元素:无法定位元素:{“method”:“css selector”,“selector”:“[name=”uID“]”} 消息:没有这样的元素:无法找到元素:{“method”:“css selector”,“selector”:“[id=”loginUsername“]”} 使用 Selenium Python 选择下拉菜单 - 无法定位元素:{“method”:“css selector”,“selector”:"[id= Selenium NoSuchElementException-无法找到元素:{“方法”:“ css选择器”,“选择器”:“ [[名称=”电子邮件地址”]”} 消息:没有这样的元素:无法定位元素:{&quot;method&quot;:&quot;css selector&quot;,&quot;selector&quot;:&quot;.pagination-shortcut-link&quot;} 点击下一页链接
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM