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
Validation 如何使用Selenium检查页面中是否存在某些文本?_Validation_Selenium_Webdriver_Assert - Fatal编程技术网

Validation 如何使用Selenium检查页面中是否存在某些文本?

Validation 如何使用Selenium检查页面中是否存在某些文本?,validation,selenium,webdriver,assert,Validation,Selenium,Webdriver,Assert,我正在使用SeleniumWebDriver,如何检查页面中是否存在某些文本?也许有人会向我推荐一些有用的资源,我可以在那里阅读。谢谢,没那么难。只需搜索包含给定文本的所有元素: List<WebElement> list = driver.findElements(By.xpath("//*[contains(text(),'" + text + "')]")); Assert.assertTrue("Text not found!", list.size() > 0);

我正在使用SeleniumWebDriver,如何检查页面中是否存在某些文本?也许有人会向我推荐一些有用的资源,我可以在那里阅读。谢谢,没那么难。只需搜索包含给定文本的所有元素:

List<WebElement> list = driver.findElements(By.xpath("//*[contains(text(),'" + text + "')]"));
Assert.assertTrue("Text not found!", list.size() > 0);

您可以如下方式检索整个页面的正文:

bodyText = self.driver.find_element_by_tag_name('body').text
self.assertTrue("the text you want to check for" in bodyText)
然后使用断言进行如下检查:

bodyText = self.driver.find_element_by_tag_name('body').text
self.assertTrue("the text you want to check for" in bodyText)

当然,您可以指定并检索特定DOM元素的文本,然后检查该文本,而不是检索整个页面。

这将帮助您检查网页中是否存在所需的文本

driver.getPageSource().contains("Text which you looking for");
Assert.IsTrue(driver.PageSource.Contains("Type your text here"));

您可以按如下方式检查页面源中的文本:

Assert.IsTrue(driver.PageSource.Contains("Your Text Here"))

Selenium 2 webdriver中没有verifyTextPresent,因此您必须检查页面源中的文本。请参见下面的一些实际示例

python 在Python驱动程序中,您可以编写以下函数:

def is_text_present(self, text):
    return str(text) in self.driver.page_source
public void verifyTextPresent(String value)
{
  driver.PageSource.Contains(value);
}
然后将其用作:

try: self.is_text_present("Some text.")
except AssertionError as e: self.verificationErrors.append(str(e))
要使用正则表达式,请尝试:

def is_regex_text_present(self, text = "(?i)Example|Lorem|ipsum"):
    self.assertRegex(self.driver.page_source, text)
    return True
请参阅:以获取完整示例

或者检查以下几个其他备选方案:

self.assertRegexpMatches(self.driver.find_element_by_xpath("html/body/div[1]/div[2]/div/div[1]/label").text, r"^[\s\S]*Weather[\s\S]*$")
assert "Weather" in self.driver.find_element_by_css_selector("div.classname1.classname2>div.clearfix>label").text
资料来源:

JAVA 在Java中,使用以下函数:

def is_text_present(self, text):
    return str(text) in self.driver.page_source
public void verifyTextPresent(String value)
{
  driver.PageSource.Contains(value);
}
其用法是:

try
{
  Assert.IsTrue(verifyTextPresent("Selenium Wiki"));
  Console.WriteLine("Selenium Wiki test is present on the home page");
}
catch (Exception)
{
  Console.WriteLine("Selenium Wiki test is not present on the home page");
}
资料来源:


比哈特 对于Behat,可以使用。它具有中定义的以下方法:

/**
*选中,则该页不包含与指定模式匹配的文本
*示例:然后我应该看到文本匹配“布鲁斯·韦恩,义务警员”
*我不应该看到“布鲁斯·韦恩,义务警员”
*
*@Then/^(?:| I)不应看到文本匹配(?P“(?:[^”]| \\”*)$/
*/
公共函数assertPageNotMatchesText($pattern)
{
$this->assertSession()->pageTextNotMatches($this->fixStepArgument($pattern));
}
/**
*检查HTML响应是否包含指定的字符串
*示例:那么回答应该包含“蝙蝠侠是高谭市应得的英雄。”
*示例:回答应该包含“蝙蝠侠是高谭市应得的英雄。”
*
*@Then/^响应应包含“(?P(?:[^“]\\”)”$/
*/
公共功能资产响应内容($text)
{
$this->assertSession()->responseContains($this->fixStepArgument($text));
}

在python中,您只需检查以下内容:

# on your `setUp` definition.
from selenium import webdriver
self.selenium = webdriver.Firefox()

self.assertTrue('your text' in self.selenium.page_source)
在c#中,此代码将帮助您检查网页中是否存在所需文本

driver.getPageSource().contains("Text which you looking for");
Assert.IsTrue(driver.PageSource.Contains("Type your text here"));
JUnit+Webdriver

assertEquals(driver.findElement(By.xpath("//this/is/the/xpath/location/where/the/text/sits".getText(),"insert the text you're expecting to see here");

如果预期文本与xpath文本不匹配,webdriver将告诉您实际文本与预期文本的对比。

Python:

driver.get(url)
content=driver.page_source
if content.find("text_to_search"): 
    print("text is present in the webpage")
下载html页面并使用find()

string\u website.py

网页中的搜索字符串

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    browser = webdriver.Firefox()
    browser.get("https://www.python.org/")
    content=browser.page_source

    result = content.find('integrate systems')
    print ("Substring found at index:", result ) 

    if (result != -1): 
    print("Webpage OK")
    else: print("Webpage NOT OK")
    #print(content)
    browser.close()
跑步

python测试网站.py

在索引处找到子字符串:26722
网页OK


d:\tools>python测试网站.py
在索引处找到子字符串:-1-1表示找不到任何东西

网页不正常

我收到通知-WebElement无法解析为类型,Assert无法解析为类型,List无法解析为类型-我需要添加一些ti导入来解决此问题?你还记得我昨天告诉你的快捷方式吗?他们帮助;-)。如果您使用的是Java,那么您应该知道问题所在。如果您使用的是Selenium库,那么您应该知道它们的位置。如果您在问题中添加了
assert
tag并使用Java,您应该知道问题的所在(需要JUnit)…很抱歉,我不熟悉selenium和Java)获取正文不是搜索页面上文本的正确方法。获取正文文本将测试源代码中是否存在该字符串,但不会真正测试该字符串是否存在于页面上。例如,可能有一个中断的PHP脚本,其中缺少一个结束?>,这意味着测试将通过,但页面将无法正确呈现。如果要断言并检查某个元素是否在某个内容中,我建议使用
self.assertIn(“要检查的文本”,bodyText)
Nice!工作起来很有魅力
AttributeError:“WebDriver”对象没有属性“getPageSource”
,以防任何人在Robot框架中查找相同的属性-这可能会有所帮助-