Reputation: 163
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>
Upvotes: 0
Views: 1314
Reputation: 17553
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 :)
Upvotes: 1
Reputation: 1195
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
Upvotes: 0