-1

I am trying to automate testing with Selenium Webdriver without the need of xpath. I'm facing problem when the site is modified then xpath is being changed. For elements(like buttons, drop downs etc) which needs some action to be performed any how it needs xpath or someother things to identify that element. If I want to fetch data(table contents) from site to validate its excecution,then here I will need lots of xpaths to do so. Is there a better way to avoid some xpaths?

Steven Henry
  • 101
  • 2
  • 13

3 Answers3

0

Instead of using xpath, you can map the elements by css selectors, like this:

driver.findElement(By.cssSelector("css selector"), OR

by ID, like this:

driver.findElement(By.id("coolestWidgetEvah")).

There are much more than these 2. See Selenium docomentation

Nimrod
  • 1,100
  • 1
  • 11
  • 27
  • I don't understand why this has been upvoted as it does what the OP wanted to originally avoid. – Xwris Stoixeia Mar 10 '17 at 15:36
  • @XwrisStoixeia My response is related to the original question, which didn't mentioned avoiding css/id, but xpath only... he changed the title after that.. – Nimrod Mar 11 '17 at 16:34
0

Steven, you basically have 2 choices the way I see it. One is to inject your own attributes (i.e qa attrib for instance) into your web elements which will never change. Please see this post, on how you can achieve this:

Selenium: Can I set any of the attribute value of a WebElement in Selenium?

Alternatively you can still use 'naked' xpath in order to locate your elements. By 'naked' i mean generic, so not so specific.

Consider this element sitting below this:

div id="username"
input class="usernameField"

button type='submit

So, instead of locating it like this (which is specific/aggresive):

//div[@id='username']//input[@class='usernameField']//button[@type='submit']

you can use a more mild approach, omitting the specific values, like so:

//div[@id]//input[@class]//button[@type]

Which is less likely to break upon change. However, beware you need to be 100% sure that with the 2nd approach you are locating a unique element. In other, words if there are more than 1 buttons you might select the wrong one or cause a Selenium exception.

I would recommend this Xpath helper add-on for Chrome which highlights on the screen when your xpath is correct and also shows you how many elements match you Xpath (i.e. unique or not?)

xpath Helper

Hope the above, makes sense, don't hesitate to ask if it does not!

Best of luck!

Community
  • 1
  • 1
Xwris Stoixeia
  • 1,831
  • 21
  • 22
0

Ofcoarse there are certain other ways without using id/xpath/CSS and even "sendKeys". The solution is to do that via Sikuli.

Things to do:

  1. You have to download the Sikuli exe jar (sikulixsetup-1.1.0). (from https://launchpad.net/sikuli/+download)

  2. Install the Sikuli exe jar which extracts the "sikulixapi" and adds to the PATH variable.

  3. Add the External jar "sikulixapi" at project level through Eclipse.

  4. Now take images of the elements where you want to pass some text or click.

  5. Use the reference of the images in Selenium Java code to write text & perform clicks.

Here is a simple code to browse to "https://www.google.co.in/" move on to Login page & enter Emailid & Password without any xpath or sendkeys.

package SikuliDemo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;

public class SikuliDemoScript {

    public static void main(String[] args) throws Exception
    {

        Screen screen = new Screen();

        Pattern image1 = new Pattern("C:\\Utility\\OP_Resources\\Sikuli_op_images\\gmailLogo.png");
        Pattern image2 = new Pattern("C:\\Utility\\OP_Resources\\Sikuli_op_images\\gmailSignIn.png");
        Pattern image3 = new Pattern("C:\\Utility\\OP_Resources\\Sikuli_op_images\\Email.png");
        Pattern image4 = new Pattern("C:\\Utility\\OP_Resources\\Sikuli_op_images\\EmailNext.png");
        Pattern image5 = new Pattern("C:\\Utility\\OP_Resources\\Sikuli_op_images\\Password.png");
        Pattern image6 = new Pattern("C:\\Utility\\OP_Resources\\Sikuli_op_images\\SignIn.png");

        System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.co.in/");

        screen.wait(image1, 10);
        screen.click(image1);
        screen.wait(image2, 10);
        screen.click(image2);
        screen.type(image3, "selenium");
        screen.click(image4);
        screen.wait(image5, 10);
        screen.type(image5, "admin123");
        screen.click(image6);

        driver.quit();      
    }

}

Let me know if this answers your question.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352