0

There is a cart link in my application. Below is the code which I get when I do inspect element. I am trying to click this element using id, xpath, linktext , partial link text..still I couldn't get through . Please help

<a id="shoppingCartLink" href="/NTNstore/cart" style="text-indent: -9px">CART</a>
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
Kumar
  • 153
  • 5
  • 12

2 Answers2

1

How to click by different ways:-

If your problem is that the element is scrolled off the screen (and as a result under something like a header bar), you can try scrolling it back into view like this:

private void scrollToElementAndClick(WebElement element) {
    int yScrollPosition = element.getLocation().getY();
    js.executeScript("window.scroll(0, " + yScrollPosition + ");");
    element.click();
}

if you need you could also add in a static offset (if for example you have a page header that is 200px high and always displayed):

    public static final int HEADER_OFFSET = 200;

    private void scrollToElementAndClick(WebElement element) {
    int yScrollPosition = element.getLocation().getY() - HEADER-OFFSET;
    js.executeScript("window.scroll(0, " + yScrollPosition + ");");
    element.click();
}

If still not work then use JavascriptExecutor

WebElement element= driver.findElement(By."Your Locator"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

I think there is a issue of frame

You need to switch to frame first. change the syntax too because answer is in C# and probably you need a java code

refer my answer in below:-

Selenium in C# - How do I navigate different frames

Hope it will help you :)

Community
  • 1
  • 1
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • thanks for your reply. I tried to capture the frame but that did not work.. Need more advise on this. However, I am able to click on the same link using 'id' in selenium IDE. But my requirement is to automate using webdriver. – Kumar Mar 14 '16 at 07:03
  • can you post your frame HTML? – Shubham Jain Mar 14 '16 at 07:19
  • Hi Shubham- There is no frame involved in the script. – Kumar Mar 14 '16 at 09:28
  • according to the error you have explained above I have updated my answer .. try that .. it will work .. one more thing use implicit wait also if you are not using it – Shubham Jain Mar 14 '16 at 09:44
0

I there is no frame and you tried with all locator strategy then use below java script code and execute using Java scripts executor class

document.getElementById("shoppingCartLink").click()

Hope this will work for you.

Thanks, Sadik

Sadik Ali
  • 1,129
  • 10
  • 26
  • There is no frame involved over there. when I use the above code, I am getting below error. Element is not clickable at point (981.5, 32). Other element would receive the click:
    – Kumar Mar 14 '16 at 09:27