0

I want to print address which is as below:
200 E Campus View Blvd.
Suite 200, Columbus, OH - 43235
+1-740-972-6457

What method I can use?
I'm using gettext() for the xpath shown in screenshot.
It throws an error as

'Unable to locate element'

.
Please see attachment enter image description here

Nitesh
  • 177
  • 7
Kishan Patel
  • 1,385
  • 3
  • 13
  • 24
  • Share the code you tried. – amitbobade Mar 14 '16 at 13:45
  • WebDriver driver = new FirefoxDriver(); driver.get("http://www.qaagility.com"); driver.findElement(By.xpath(".//*[@id='main_header']/nav/ul/li[5]/a")).click(); Thread.sleep(3000L); WebElement a =driver.findElement(By.xpath("html/body/div[1]/div/div/section/article/div[3]/div[4]/div[2]/div[2]")); Thread.sleep(3000L); String b = a.getText(); System.out.println(b); – Kishan Patel Mar 15 '16 at 04:03

3 Answers3

1

Solution:-

    driver.get("http://qaagility.com/");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    driver.findElement(By.xpath(".//*[@id='main_header']/nav/ul/li[5]/a")).click();

    WebElement elements = driver.findElement(By.xpath("//div[@class='contact_container'][1]/div[@class='contact_us'][2]/div[@class='address']"));

     JavascriptExecutor js = (JavascriptExecutor) driver;
     int yScrollPosition = elements.getLocation().getY();
     js.executeScript("window.scroll(0, " + yScrollPosition + ");");

    String address = elements.getText();
    System.out.println(address);

Use below XPath:-

//div[@class='contact_container']//div[@class='address']

Use below code:-

String address = driver.findElement(By.xpath("//div[@class='contact_container']//div[@class='address']")).getText();
System.out.println(address);

If still not work then there should be a frame present on your DOM

You need to switch to frame first.

refer my answer in below:-

Selenium in C# - How do I navigate different frames

change the syntax because answer is in C# and probably you need a java code

Hope it will help you :)

Community
  • 1
  • 1
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • No error. But nothing is printed under syso(address). – Kishan Patel Mar 15 '16 at 04:57
  • GET IN TOUCH WITH US.
    There is a iframe used.
    – Kishan Patel Mar 15 '16 at 05:13
  • Iframe is not having any ID. i used driver.switchTo().frame() Still not working. – Kishan Patel Mar 15 '16 at 05:13
  • you need to switch by following method described in the link :- http://stackoverflow.com/questions/35170447/selenium-in-c-sharp-how-do-i-navigate-different-frames – Shubham Jain Mar 15 '16 at 05:46
  • All under default. Still not able to locate. My scenario 1) go to qaagility.com 2) go to contact us 3) print the usa address on console – Kishan Patel Mar 15 '16 at 10:28
  • I have added the code for you in my answer .. It is working fine .. njoy ;) – Shubham Jain Mar 15 '16 at 12:00
  • I Did. Working fine. Thank you.. Can you explain me what is scrollToElement method doing? – Kishan Patel Mar 15 '16 at 12:27
  • This method move to your page scroll on the element so selenium can perform their action, somtimes selenium can't able to perform if element is down or up side on the page .. it happens some time .. that's why I created separate function so you can call it again if found similar issue again .. have a nice day :) – Shubham Jain Mar 15 '16 at 12:31
  • You Used just getY(). why so ? Can we use getX(); or both ? – Kishan Patel Mar 16 '16 at 03:27
  • One more question. I'm using TestNG , So i wrote the main method as @Test. But i can't write scrollelement method as Test. So what should i use for that scrollelement method ?? – Kishan Patel Mar 16 '16 at 03:40
  • only getY because your page needs to scroll up and down.. if there is any situation in which your page also have horizontal scroll then you can change that value. – Shubham Jain Mar 16 '16 at 03:41
  • why do you need to add that function as Test.. You have already call it in your script that why we have created object of that class and callled that funtion .. just call that function similarly whenever you need in future – Shubham Jain Mar 16 '16 at 03:42
  • or if you are facing the problem then I have remove that function and added that code in between the script .. updated my answer – Shubham Jain Mar 16 '16 at 03:49
  • Excellent. Got it. Thank you... I'll come to you with more queries .. :-) :-) :-) thanks a lot... – Kishan Patel Mar 16 '16 at 03:54
  • i don't have 15 reputation. so it is not happening. – Kishan Patel Mar 16 '16 at 11:52
0

Use this css selector to get the text: ".center.slide_wrapper > div:nth-child(4) > div:nth-child(2)"

irb#1(main):039:0* find(".center.slide_wrapper > div:nth-child(4) > div:nth-child(2)").text
=> "USA 200 E Campus View Blvd. Suite 200, Columbus, OH - 43235 +1-740-972-6457 usa@qaagility.com"

if you are using Python or Ruby use .text if Java use .getText() and C# use .Text().

Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
  • @KishanPatel What iş your output? See my log it is working. You may need to add some waiting time to have dom ready. – Mesut GUNES Mar 15 '16 at 05:14
  • I'm using .getText() as I'm running on java. The test case is passed, but in output it is not showing the address. – Kishan Patel Mar 15 '16 at 07:12
  • String c = driver.findElement(By.cssSelector(".center.slide_wrapper > div:nth-child(4) > div:nth-child(2)")).getText(); i used this under eclipse . I debugged it. string c is not getting anything. – Kishan Patel Mar 15 '16 at 07:17
  • @KishanPatel are you sure that you are waiting until the dom is ready. Css seems correct, i did it successfully. – Mesut GUNES Mar 15 '16 at 07:27
  • Still not working.. I'm getting restless. My scenario : go to qaagility.com go to contact us print the usa address on eclipse console can u send me code? please – Kishan Patel Mar 15 '16 at 09:17
0

Here is the working C# code to get US Address.

[Test]
    public void TestMethod1()
    {
        IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(@"ChromeDriver\Path");

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));

        driver.Navigate().GoToUrl(@"http://www.qaagility.com/");

        driver.Manage().Window.Maximize();

        //Wait for Contact US link in header. 
        IWebElement contactUS = wait.Until<IWebElement>((d) => { return driver.FindElement(By.LinkText("Contact Us")); });
        contactUS.Click();

        //Wait for Address
        IWebElement USAddress = wait.Until<IWebElement>((d) => { return driver.FindElement(By.CssSelector("div[class='center slide_wrapper']>div:nth-of-type(4)>div:nth-of-type(2)>div:nth-of-type(2)")); });
        Console.WriteLine(USAddress.Text);
    }

Please note that US Address isn't under iFrame. iFrame is for Google Map. Try above code in Java.

Here is the Output of the above code.

enter image description here Hope this helps :)

amitbobade
  • 500
  • 1
  • 4
  • 15
  • Can you guide me for eclipse?? Same i'm writing in eclipse it is not working. please help.. – Kishan Patel Mar 15 '16 at 07:22
  • String c = driver.findElement(By.cssSelector(".center.slide_wrapper > div:nth-child(4) > div:nth-child(2)")).getText(); This is not working.. string c is returning null – Kishan Patel Mar 15 '16 at 07:26
  • Change syntax accordingly. Use explicit wait as - 'wait.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));' – amitbobade Mar 15 '16 at 07:26
  • Use CSS Selectors as it is. – amitbobade Mar 15 '16 at 07:27
  • Still not working.. I'm getting restless. My scenario : go to qaagility.com go to contact us print the usa address on eclipse console can u send me code? please – Kishan Patel Mar 15 '16 at 09:16