2

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>
5
  • Welcome to the site. In order for us to help, can you add the code of the website you are testing that has this text? Can you put your code you've attempted to use to try to solve this? What else have you done to solve this issue? What language are you using with Selenium? The more context you provide, the easier it is to help.
    – Lee Jensen
    Commented Nov 14, 2019 at 21:22
  • Thanks for your comments, actually I have given my code snippet only. I am trying to automate an angular webpage with Selenium / Java. Commented Nov 14, 2019 at 22:15
  • Have you tried a simple getText? Commented Nov 14, 2019 at 22:49
  • You may be interest in this: sqa.stackexchange.com/questions/18438/…
    – corsiKa
    Commented Nov 14, 2019 at 23:23
  • Thanks for your comments again, i am able to get the text inside the div tag but the problem now is locating the div tag :(. And yeah, gone thru protractor and it is not helping that much with my application due its very dynamic changes. Commented Nov 15, 2019 at 1:31

2 Answers 2

3

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:

  1. Extract the tag assuming there is no places where divs have "Version" word:var version = element(by.xpath('//div[contains(., 'Version:')]'));

  2. 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.

2
  • Don't you already have the whole version number by line 1 here? :)
    – anonygoose
    Commented Nov 15, 2019 at 16:21
  • 1
    Yup, but word "version" is rarely a part of version value itself, so if taken from other source it would be better to have only version value to compare since there it could be version (from the lowercase) or ver or ver. etc.
    – wec
    Commented Nov 15, 2019 at 16:26
1

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.*;
1
  • When downvoting at least tell the reason
    – PDHide
    Commented Nov 17, 2019 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.