0

I've executed my code in selenium-java and i need a value of Identifiers(2nd line, not the 4th one) from .XML which is opened in MS edge browser.

My XML:

<Application Type="ABCD">
<Identifier>18753</Identifier>
<SalesChannel SalesChannelType="PQRS" SalesChannelSegment="XYZ">
<Identifier>AB1234</Identifier>

Can anyone help me with the code to get a values(18753) which is between Identifier 2nd line. Note: I've a code which is working fine for chrome & FF, but bot able to work for MSedge:

Assert.assertTrue(driver.getPageSource().contains("Identifier"));   
String xml =  driver.getPageSource();
String appID = xml.split("<Identifier>")[0].split("</Identifier>")[1];
elvira.genkel
  • 1,303
  • 1
  • 4
  • 11
Omi
  • 1
  • 1

1 Answers1

0

I think there's a mistake in your split function. The right code should be like this:

String appID = xml.split("</Identifier>")[0].split("<Identifier>")[1];

Sample code:

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;


public class Edgeauto {
    public static void main(String[] args) { 
        System.setProperty("webdriver.edge.driver", "your_path_of_webdriver\\msedgedriver.exe"); 
        EdgeOptions edgeOptions = new EdgeOptions();
        WebDriver driver = new EdgeDriver(edgeOptions); 
        driver.get("http://xxx.xml");

        Assert.assertTrue(driver.getPageSource().contains("Identifier"));   
        String xml =  driver.getPageSource();       
        String appID = xml.split("</Identifier>")[0].split("<Identifier>")[1];    
        System.out.println(appID);
    }
}

Result:

enter image description here

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
  • Hi YU Zhou,Thanks for your reply. But Unfortunately i am not able to add edgeOptions.addArguments("--disable-features-RendererCodeIntegrity"); in my code. And when I do String xml = driver.getPageSource(); it's opening html for me and giving me an attribute value instead of APPID – Omi Apr 30 '20 at 12:43
  • That line is not necessary in the code. Besides, which version of Edge are you using? Are you using Edge Legacy or Edge Chromium? – Yu Zhou May 04 '20 at 01:49
  • I am using Edge legacy. But I got the answer, to handle .XML file in IE/Chrome browser only driver.getPageSource won't work have to use below line: "driver.findElement(By.tagName("Identifier")).getText();" String xml = driver.getPageSource(); if (sBrowserName.equals("internet explorer")){ String pageSource1=driver.findElement(By.tagName("Identifier")).getText(); appID = pageSource1.split("")[1].split("")[0]; } else{ appID = xml.split("")[1].split("")[0]; } – Omi May 11 '20 at 00:04
  • Great! You could put your solution as an answer and mark it as an accepted answer. It can help other community members in future in similar kind of issues. Thanks for your understanding. – Yu Zhou May 11 '20 at 01:38