无法定位元素:{“方法”:“css选择器”,“选择器”:“.mr508”}

cngwdvgl  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(328)

我正在尝试从instagram获取评论用户名和他们的评论。但我在username和user_评论中只得到用户名。
我的代码如下:

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>
nfs0ujit

nfs0ujit1#

您可以尝试使用下面的xpath来获取 user comment 来自instagram。

//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

相关问题