簡體   English   中英

如何使用javascript硒獲取父div ID?

[英]how to get the parent div ID using javascript selenium?

我正在使用webdriverjs,我想獲取一個webelement的父ID。有多個稱為row的類,但是每個類都有不同的ID。 挑戰在於發送消息時會生成ID。 所以我可以按類獲取innerHtml,然后嘗試獲取ID。 我有下面的HTML,

<div class="row" id="4003">
<div class="client-chat">
<p class="client-chat-text">If you have any questions, don’t hesitate to message me. I’m here to help with whatever you need!<span class="chat-time">Feb 01 2017 11:30:57</span></p>
</div>
</div>

<div id="4361" class="row">
<div class="coach-chat">
<p class="coach-chat-text">
hi
</p>
</div>

與id(4361)一起的第二個div是生成的div,我需要對其進行測試。 但是,我能夠獲取“ 教練聊天”文本 我如何獲得硒中的父元素。 我已經嘗試了以下代碼,但是我不知道如何獲取父ID。

it('send a text message now',function(done){
            driver.findElement(webdriver.By.css("#messageField")).sendKeys('Hi');
            driver.sleep(1000);
            driver.findElement(webdriver.By.xpath(".//*[@id='sendMessage']")).click().then(function(){
                driver.findElement(webdriver.By.className("coach-chat-text")).getText().then(function(text){
                    var message = text.slice(0,2);
                    //i want to fetch the parent id here.
                    try {
                        expect(message).to.equal("www.pluma.co");
                        done();
                    } catch (e) {
                        done(e);
                    }
                });
            });
        });
    });

如果使用webdriver.By.xpath查找具有“ coach-chat-text”類的元素,則可以使用XPath父選擇器- /parent::node() -查找父元素。

如下所示,盡管您的XPath可能有所不同:

driver.findElement(webdriver.By.xpath(".//*[contains(@class, 'coach-chat-text')]/parent::node()"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM