Using Selenium2, How to Check If Certain Text Exists on the Page

Using Selenium2, how do I check if certain text exists on the page?

WebElement bodyTag = driver.findElement(By.tagName("body")); 
if (bodyTag.getText().contains("Text I am looking for") {
// do something
}

or find a speific div

or you can use the Selenium class WebDriverBasedSelenium and do something like

var selenium=new WebDriverBasedSelenium(driver,url);

selenium.IsTextPresent("text")

C# Selenium, how to check that text exists on page <div>

It seems that the text is present inside your div tag, so you can just do:

var element = driver.FindElement(By.Xpath("your_xpath"));
if(!string.IsNullOrEmpty(element.Text))
{
//the text is present
//do your stuff here
}

EDIT to clarify the questions from the comment:
string.IsNullOrEmpty will return a boolean value: true if there's no value for the text (null or empty) and false if there are string characters in your text value. This includes whitespaces too, so if you want to make sure that you don't have a whitespaces only text you could also use string.IsNullOrWhitespace() that is a more deeper check than the previous one.
So, by reversing it using ! (not operator) you're basically saying that if you have something in your text value, do other actions.

Now, to assert that the text is indeed the correct one, you use the Text property of the IWebElement interface, property that contains the text value between the opening and closing of your html tag.
In code this would translate to:

//exact text
var element = driver.FindElement(By.Xpath("your_xpath"));
if(!string.IsNullOrEmpty(element.Text) && element.Text == "your_text")
{
//the text is present
//do your stuff here
}

//partial text
var element = driver.FindElement(By.Xpath("your_xpath"));
if(!string.IsNullOrEmpty(element.Text) && element.Text.Contains("your_text"))
{
//the text is present
//do your stuff here
}

You need to preserve the order in your if statement because if the text value is null, when you're applying Contains() on it, it will result in a Object reference exception.

Check if text exists on a page, but read the text from a variable (Selenium)

You need to pass the video title as a variable instead of as text, as displayed in the code below:

video_title = 'Michael Jackson - Thriller'
driver.find_elements_by_xpath("//*[contains(text(), '" + video_title + "')]")

Note the extra single quote around video_title, required because it's a string. At runtime this will result in the following XPath expression:

//*[contains(text(), 'Michael Jackson - Thriller')]

How can I check if some text exist or not in the page using Selenium?

With XPath, it's not that hard. Simply search for all elements containing the given text:

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

The official documentation is not very supportive with tasks like this, but it is the basic tool nonetheless.

The JavaDocs are greater, but it takes some time to get through everything useful and unuseful.

To learn XPath, just follow the internet. The spec is also a surprisingly good read.


EDIT:

Or, if you don't want your Implicit Wait to make the above code wait for the text to appear, you can do something in the way of this:

String bodyText = driver.findElement(By.tagName("body")).getText();
Assert.assertTrue("Text not found!", bodyText.contains(text));

Trying to find if certain text exists

Use regular expression re to check the text exist or not.Here is your code.

from selenium import webdriver
from bs4 import BeautifulSoup
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re

#Chrome webdriver filepath...Chromedriver version 74
driver = webdriver.Chrome(r'C:\Users\mfoytlin\Desktop\chromedriver.exe')
page = driver.get('https://www.zillow.com/lender-profile/zackdisinger/')
show_more_button =WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'Show')][contains(.,'more')]")))
#driver.execute_script("arguments[0].click();", show_more_button)
show_more_button.click()
time.sleep(2)
soup = BeautifulSoup(driver.page_source, 'html.parser')


if soup.find(text=re.compile('Nationally registered')):
print('Success')
else:
print('False')

It is printing success on console.

Success

Best way to check if text exist or not in the page using Selenium- Python

You could do a few things to solve this problem, using Xpath in both cases.

You can use text() like this //div[text()='your text'], this should return your element. Also, you can match the style using @style, like this //div[@style='width:161.75mm;min-width: 161.75mm;']

Here is a link to a great, useful Xpath cheatsheet!

If you're using selenium, it would work like this.

driver.find_element_by_xpath('xpath here')


Related Topics



Leave a reply



Submit