Python 2.7 Selenium Python:使用find_element_by_css_selector()方法从子元素中删除文本
在使用Python的Selenium中,我一直在尝试从具有子元素的DIV选择器中选择文本。代码如下所示:-Python 2.7 Selenium Python:使用find_element_by_css_selector()方法从子元素中删除文本,python-2.7,selenium,methods,selenium-webdriver,css-selectors,Python 2.7,Selenium,Methods,Selenium Webdriver,Css Selectors,在使用Python的Selenium中,我一直在尝试从具有子元素的DIV选择器中选择文本。代码如下所示:- <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</butto
<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>
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()
我无法找到任何可以直接从我使用的方法中删除/取消选择“按钮”文本的内容。您可以使用以下xpath:
//div[@class='empty-cart-message' ]/child::text()
但是,目前selenium不支持object(因为它不是webelement),您可以通过Python javascript executor运行:
returnText = driver.execute_script("return document.evaluate(\"//div[@class='empty-cart-message']/child::text()\", document, null, XPathResult.STRING_TYPE, null).stringValue;")
正如@mahsum-akbas所说(他的回答如上),我尝试使用javascript executor。但是,方法
driver.execute\u script()
没有返回任何值。我在本地尝试了相同的html和python,可以在输出中看到文本。我也使用了@mahsum-akbas!!脚本正在运行,没有显示任何错误,但是execute\u script()
返回一个空白字符串。它是否需要在特定版本的Selenium或Python或您可能使用的任何不同浏览器上进行测试?