3

I am trying to click on a web element using Selenium WebDriver(2.21.0).

When I try driving through the Selenium IDE it works properly but when I try the same set of actions using the Java implementation of Firefox driver- it leads to the wrong page.

While the code is running and I manually scroll to the desired element, it works.

I am making sure that the web element is visible and enabled using

By by = By.xpath("(//a[contains(@href, 'javascript:void(0);')])[26]"); //**Edit:** this is how i  
                                                                       //am getting the locator

WebElement element = driver.findElement(by);


return (element.isEnabled() || element.isDisplayed()) ? element : null;

which returns some element but not the one I am expecting.

This looks strange to me as Selenium webdriver mostly scrolls to an element(if not visible on the screen) by itself and does the required interaction.

I have tried some answers like one, two with no success.

Thanks in advance!

EDIT: here is the IDE's exported code(java/JUnit4/webdriver)

package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Bandar {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://e.weibo.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testBandar() throws Exception {
        driver.get(baseUrl + "/nescafechina");
        driver.findElement(By.xpath("(//a[contains(@href, 'javascript:void(0);')])[26]")).click();
        driver.findElement(By.xpath("(//a[contains(@href, 'javascript:void(0);')])[12]")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}
Community
  • 1
  • 1
Ishank
  • 2,860
  • 32
  • 43
  • Can you paste the export in here for what the Selenium IDE is doing? – Curtis Miller Sep 05 '12 at 15:15
  • Thanks for the response Mr.Curtis! I have pasted Selenium IDE's exported Test case. – Ishank Sep 06 '12 at 05:34
  • Seems to me that I have to give a better locator than the ones created by the Selenium IDE. I have tried the XPath locator generated from FirePath and Firebug without any success. – Ishank Sep 06 '12 at 06:41

1 Answers1

1

Ishank,

What I have done is gone through and created a test that shows the different kind of asserts that you can use in your testing. They do look a little different from what you are looking at, I feel your main problem is WebElement element = driver.findElement(by); because you are not giving it an actual element. The (by); section is looking for a string that can be found on the page. Acceptable strings would be; id("gbfqb"); or xpath("(//a[contains(@href, 'javascript:void(0);')])[26]"); or even name("find-button");.

/**
 * Test the main Google page.
 * @throws InterruptedException 
 * 
 */
@Test
public void signUp() throws InterruptedException {

String testId = "TestStack01";

entered(testId);

webDriver.get("www.google.com");
webDriver.findElement(By.id("gbqfq")).clear();
webDriver.findElement(By.id("gbqfq")).sendKeys("Test");
assertEquals("", webDriver.findElement(By.id("gbqfb")).getText());

WebElement whatyourlookingfor = webDriver.findElement(By.id("gbqfb"));
assertTrue(selenium.isElementPresent("gbqfb"));
assertTrue(whatyourlookingfor.isEnabled());
assertTrue(whatyourlookingfor.isDisplayed());
assertFalse(whatyourlookingfor.isSelected());

webDriver.findElement(By.id("gbqfb")).click();

leaving(testId);

}

I hope that this has helped in getting which element is being returned.

Curtis Miller

Curtis Miller
  • 580
  • 1
  • 9
  • 31
  • 1
    Hey Curtis, thanks for ur time! I have edited the code snippet, I have been obtaining the 'by' from- By by = By.xpath("(//a[contains(@href, 'javascript:void(0);')])[26]"); so, I hope that the correct Locator goes in driver.findElement(by); .. – Ishank Sep 07 '12 at 06:55
  • It looks like the javascript in the Xpath is what could be causing the issue. You might want to try something like 'selenium.fireEvent("name=title", "blur"); ' after the javascript action is being performed. – Curtis Miller Sep 08 '12 at 05:50