How can I get this Version: 0.1.0 - 94
value from the <div>
tag in selenium webdriver? and here is my code:
<div _ngcontent-qbk-c3="" style="text-align: center;">Version: 0.1.0 - 94</div>
How can I get this Version: 0.1.0 - 94
value from the <div>
tag in selenium webdriver? and here is my code:
<div _ngcontent-qbk-c3="" style="text-align: center;">Version: 0.1.0 - 94</div>
As far as I can understand, your issue is to extract version number to use it further in the test. So unless you have some context you could go from in order to build proper and reliable xpath, I would suggest to use following approach:
Extract the tag assuming there is no places where div
s have "Version" word:var version = element(by.xpath('//div[contains(., 'Version:')]'));
Get full version text and parse out the required part (example can be found here)
Above is the example for protractor. If we are talking about Java, then you could use something like this:
String versionNumber = driver.findElement(By.xpath("'//div[contains(., 'Version:')]")).getText();
Pattern pattern = Pattern.compile("^Version:\\s(.+)$");
Matcher m = pattern.matcher(versionNumber);
String clearedVersion = "";
if(m.find()){
clearedVersion = m.group(1);
}
System.out.println(clearedVersion);
The code above preserves version number to the clearedVersion
variable.
version
(from the lowercase) or ver
or ver.
etc.
Hi using protractor does not give any advantage in this case other than avoiding explicit waits in the tests ( That's a huge advantage when you have a large number of tests ). In addition, protractor uses selenium web driver for interacting with browsers so it's just a new approach of using selenium and is not a completely different test automation tool.
Coming to your problem, I am not sure whether the element is always present or there should be a user action like a button click that makes the element appear in the DOM.
Assuming the latter case, you can use an explicit wait for the element to appear before trying to get the text
//Creating a By locator object
By locator=By.xpath("//a[contains(text(),\"Version\")]");
//Creating a webdriver wait (waits for max 20 sec for expected condition)
WebDriverWait wait = new WebDriverWait(browser, 20);
//Creating a regex pattern
Pattern p = Pattern.compile(".*Version.*");
//Waiting for the element which contains "Version" to be present, This returns true or false.
Boolean status = wait.until(ExpectedConditions.textMatches(locator, p));
//Asserting that it should be true, else fail the test as the element was not present
assertTrue(status);
//If the element is present get The Text
if(status) {
WebElement element = browser.findElement(locator);
String value=element.getText();
System.out.println(value);
}
The packages used are:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.*;