2

I am pretty much new to selenium. I have referred previous posts and found out how to click an element using javaScriptExecutor. But unfortunately, it is not working and i can;t where I am going wrong. Below is the code snippet and some screenshots:

This is how i found out the Xpath in chrome:

Code :

    @Test
public void Search(){
    try{

        WebElement element = driver.findElement(By.xpath("//a[contains(@href,\"javascript:__doPostBack('lbSearch','')\")]"));
        JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript("arguments[0].click()", element);
    }
    catch(Exception e){
        System.out.println("Search element not found."+ e.getStackTrace());
    }

Result :

Thankx in advance!

YoMan
  • 39
  • 5

1 Answers1

0

Instead of JavaScriptExecuter you can also click normally by selenium function library.

Your xpath will be //*[@id='lbSearch']

You can click using below code,

@Test
public void Search(){
     WebElement element = null;
    try{

       element = driver.findElement(By.xpath("//*[@id='lbSearch']"));
    }
    catch(Exception e){
        System.out.println("Search element not found."+ e.getStackTrace());
    }
    element.click();
}

One more thing you have to use implicit and explicit wait for locate element. this link will help you.

Community
  • 1
  • 1
Sanjay Bhimani
  • 1,593
  • 1
  • 15
  • 29
  • Have you initialize your driver properly? Are you getting error? then provide me error log. – Sanjay Bhimani Feb 08 '16 at 05:24
  • @Sanjay..here is my code...`public String baseUrl = "URL"; public WebDriver driver; @BeforeSuite public void beforeTestSearch(){ System.setProperty("webdriver.chrome.driver", "C://Users/navyatha.devaraju/Downloads/chromedriver_win32/chromedriver.exe"); driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS); ` – YoMan Feb 08 '16 at 05:27
  • Script has not issue. Issue is with chromedriver and request-response. Look at this que, http://stackoverflow.com/questions/25080500/when-run-webdriver-with-chrome-browser-getting-message-only-local-connection . – Sanjay Bhimani Feb 08 '16 at 05:37
  • If not works then remove your try-catch block and run your script again, Then provide error log if there is another problem. – Sanjay Bhimani Feb 08 '16 at 05:45
  • @Sanay...i tried the same code in firefox..i am getting the same error!! – YoMan Feb 08 '16 at 06:32
  • It is just informational message. After remove try-catch block run your script and show error log for findElement. then let me know. – Sanjay Bhimani Feb 08 '16 at 06:43
  • @Sanjya...I am getting the same exception message on the console..as in the screenshot.."Search Element not found.." – YoMan Feb 08 '16 at 07:38
  • It is a locator issue. Make sure your locator is visible before timeout and unique. If timeout occurs then try also Thread.sleep(--) before locating element. Locate with different strategy like class id xpath or even through attribute. Eg, By.xpath("//*a[@class='topMenuLink']") , By.linkText("//*a[@id='Search']") , By.xpath("//*a[contains(@href,'lbSearch')]"). But make sure whatever locator you make , it should be unique and visible. – Sanjay Bhimani Feb 08 '16 at 07:45
  • FAILED: Search org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//a[contains(@href,\"javascript:__doPostBack('lbSearch','')\")]"} Command duration or timeout: 45.11 seconds – YoMan Feb 08 '16 at 07:49
  • Your xpath syntext is wrong, It should be in this format. //a[contains(@href,'')]. There is no need to enter every character. just enter unique character , it uses contains so it will take it. – Sanjay Bhimani Feb 08 '16 at 07:56