Selenium WebDriver GetPageSource().Contains("")
Solution 1
Lets go one by one:
driver.getPageSource()
Here driver
is an object ob WebDriver class . getPageSource()
is method of WebDriver class.
So driver.getPageSource()
returns source code of the page which stored as string. contains
is method of a String class to check if a string contains in another string.
So, driver.getPageSource().contains("Text to find");
will return True if 'Text to find' is found in page source code else False.
Hope it helps!
Solution 2
The mentioned code is equivalent of -
String pageSource = driver.getPageSource(); //after loading some url through driver.get()
boolean result = pageSource.contains("Text to find");
Here, driver
is your WebDriver
object from which you call getPageSource()
(javadoc) method which returns a String
object containing the source code of the loaded website.
Now on that returned String
object, you are calling contains()
method which checks whether the String
includes(or contains) the text you passed in the argument.
Solution 3
From the doc Class RemoteWebDriver
Get the source of the last loaded page. If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page.
The page source returned is a representation of the underlying DOM: do not expect it to be formatted or escaped in the same way as the response sent from the web server. Think of it as an artist's impression
getPageSource() --> return the source of the current page .
Contains()--> we check that word presents in that page or not
Nit QA
Updated on December 06, 2022Comments
-
Nit QA over 1 year
What is the full meaning of below piece of code? I mean, which object implementing which class and using which method?
driver.getPageSource().contains("Text to find");