Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果元素中的文本包含特定的部分文本,则断言_Python_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 如果元素中的文本包含特定的部分文本,则断言

Python 如果元素中的文本包含特定的部分文本,则断言,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我在标签中有一个元素,它有发行说明。我必须验证它是否包含特定的文本。我能够使用以下代码提取文本: webdriverwaitows.driver,10.untelec.visibility\u元素\u locatedBy.XPATH,ManageSoftware.release\u notes\u XPATH.get\u attributeinnerHTML 我如何断言它是否包含特定的文本,比如abc。这里有没有像contains或isPresent这样的函数可以使用 我正在编写的代码是: &l

我在标签中有一个元素,它有发行说明。我必须验证它是否包含特定的文本。我能够使用以下代码提取文本:

webdriverwaitows.driver,10.untelec.visibility\u元素\u locatedBy.XPATH,ManageSoftware.release\u notes\u XPATH.get\u attributeinnerHTML

我如何断言它是否包含特定的文本,比如abc。这里有没有像contains或isPresent这样的函数可以使用

我正在编写的代码是:

<div id="dialog" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: 483px;">
<pre> 
Text is here.
</pre>
</div>

要验证任何元素中是否存在所需的文本,您需要使用try-catch{}块为text_To_be_present_in_元素创建WebDriverWait,您可以使用以下任一选项:

使用CSS_选择器:

使用XPATH:

注意:您必须添加以下导入:


是的,但我不知道如果不使用selenium中的xpath函数是否可能。以下内容将在此处找到包含文本的pre.:driver.find_element_by_xpath//pre[contains.,“Text is here.”感谢您的回复。这对我有用。唯一的问题是我正在验证的发行说明太长,如果我使用不存在的错误关键字验证测试失败,那么它会抛出超时异常。我不确定是否有办法加快搜索过程,以给出一个失败的答案。还要确认您给出的解决方案是检查发行说明中是否包含预期的文本?这就是验证部分文本匹配?@nd23如果这个/任何答案对您有帮助,对将来的读者都有帮助,请回答。@nd23您可以用一个包含较小/较大文本的变量替换字符串文本。很抱歉,我不太理解这个评论。在我的解决方案中,我使用以下选项:element=WebDriverWaitdriver,20.untelec.text_to_be_present_In_elementBy.CSS_选择器,div.ui-dialog-content.ui widget contentdialog>pre,验证它是否包含在文本中的文本在这里。我的意思是,如果我检查一个测试失败,它会花费太长时间。有没有办法让搜索更快?@nd23检查更新的答案,如果您有进一步的疑问,请告诉我。
try:
    WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, "div.ui-dialog-content.ui-widget-content#dialog>pre"), "Text is here"))
    print("Desired text was present")
except TimeoutException:
    print("Desired text was not present")
try:
    WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element((By.XPATH, "//div[@class='ui-dialog-content ui-widget-content' and @id='dialog']/pre"), "Text is here"))
    print("Desired text was present")
except TimeoutException:
    print("Desired text was not present")
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException