0

硒使用find_element_by_css_selector從子元素消除文本()方法中,我一直在試圖選擇具有子元素的DIV選擇文本well.The代碼如下所示: -硒的Python:使用Python

<div class="empty-cart-message"> 
There are no items in this cart.<br> 
<button class="btn btn-blue close continue-button continue-empty-btn tmargin10">Continue Shopping</button> 
</div> 

我需要文本「此購物車中沒有物品。」只是,我正在使用find_element_by_css_selector()方法。

我面臨的問題是,除了我需要的文本外,它還獲取了按鈕標籤中的文本。我使用看起來像這樣的代碼: - 我無法找到任何可以消除/取消

cart_status=driver.find_element_by_css_selector("div.empty-cart-message").text 

if(assertEqual("There are no items in this cart", cart_status)): 
    cont_btn=driver.find_element_by_css_selector("button.continue-empty-bin") 
    cont_btn.click() 

「按鈕的」直接從我使用的方法文本。

回答

0

您可以使用以下XPath:

//div[@class='empty-cart-message' ]/child::text() 

但是,目前硒不支持對象(因爲它不是一個webelement),您可以通過Python的運行JavaScript執行:

returnText = driver.execute_script("return document.evaluate(\"//div[@class='empty-cart-message']/child::text()\", document, null, XPathResult.STRING_TYPE, null).stringValue;") 
+0

作爲告訴@ mahsum-akbas(他的回答上面),我嘗試使用JavaScript executor.But,**方法**'driver.execute_script()'返回**沒有值** –

+0

我在我的本地嘗試了相同的html和python並且可以在輸出中看到文本。 –

+0

我做了同樣的@ mahsum-akbas!腳本運行時沒有顯示任何錯誤,但'execute_script()'返回一個空白的** String **。它是否需要在您可能使用的特定版本的** Selenium或Python或任何其他瀏覽器**上進行測試? –