Can not click on a button; - "element is not interactable" - java

I have a script where I'm trying to push a button on an ecommerce site but when I run a script I get "Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable " .
When I put xpath into the chropath, element is located so I guess path driver.findElement(By.xpath("//form/fieldset[2]/button")).click(); is right. Please see attached screenshots and be gentle I'm new to programming and this site :/

I see, there's an Accept cookies button, if you want to click on that, you can use the below code :-
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();
and then click on Sleeve length like this :
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//form/fieldset[2]/button"))).click();
Note that you should open browser in full screen before interacting any web element.
driver.manage().window().maximize();
driver.get("Your URL");

You could use a JavascriptExecutor to trigger the click:
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", yourWebElement);
But I would recommand also to use explicit/implicit waits instread of Thread.sleep (I've linked an article, you can find more information on google).

Related

Xpath issue in element location

I am getting a very long xpath for an element that I selected. Is there anyway to shorten it? This is the xpath I am getting:
//li[#class='menu_men 1-level hasChild']//div[contains(#class,'level-2')]//div[#class='menu-wrapper']//ul[#class='level-2']//li[#class='1-level']//div[#class='level-3']//ul[#class='level-3']//li//a[#class='level-3'][contains(text(),'Socks')]
This is the URL: Calvin Klein Singapore I hovered over 'MEN', the accessories section will appear, than I hover the 'Socks' to get the xPath.
I am getting the following execption in my code and I am wondering if somehow the long xpath could be one of the reasons:
org.openqa.selenium.NoSuchElementException: no such element: Unable to
locate element: {"method":"xpath","selector":"//li[#class='first
menu_men 1-level
hasChild']//div[contains(#class,'level-2')]//div[#class='menu-wrapper']//ul[#class='level-2']//li[#class='1-level']//div[#class='level-3']//ul[#class='level-3']//li//a[#class='level-3'][contains(text(),'Socks')]"}
I am using cropath from within chrome developer tools to get the xPath.
I am new to automation, I really hope someone can advise. Thank you.
#SameerArora this is the code I have to clear the pop up window, as what I had mentioned in the comments below.
//for clearing the popup window
#FindBy(how=How.XPATH,using="//*[starts-with(#id,'popup-subcription-closes-link-')]")
public WebElement newsletterpopup;
public String clickCategory(){
//.....
resusableFunctions.buttonClick(driver, newsletterpopup, "popoup");
}
public void buttonClick(WebDriver driver, WebElement element, String elementName) throws InterruptedException
{
try
{
element.click();
System.out.println("Log: ResuableFunction.buttonClick");
}
catch (org.openqa.selenium.ElementNotInteractableException notInteract)
{}
The element you are looking for can be found using xpath:
WebElement element = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
However, as the element is not visible directly when you are opening the link, you would be getting NoSuchElementException, so to resolve it you can use javascript click method on the element which directly operates on the div of the page.
Addition to this, i can see that a subscription popup comes when i am opening the page for the first time, so you need to dismiss that popup first(if the popup is present) and then click on the "Socks" element using the JavaScript click method.
Your code should be like:
List<WebElement> closeSubscriptionPopUp = driver.findElements(By.xpath("//a[contains(#id,'popup-subcription-closes-link')]"));
if (closeSubscriptionPopUp.size() > 0) {
closeSubscriptionPopUp.get(0).click();
}
WebElement sockElement = driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", sockElement);
To hovered over 'MEN' >> accessories >> 'Socks' section, You need to use selenium Actions class.
As it is not really possible to first click on men(as it will open other section),
So to hover to sock, you need to chain all of the actions that you want to achieve in one go.
Process should be:
move to men element first
Move to accessories
then move to Socks and click on it.
Note: By using Action class, we can chain all the process in one single go.
As mentioned below
1) First way:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]")))
.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")))
.click().build().perform();
2) Second way with wait:
WebDriverWait wait= new WebDriverWait(driver, 10);
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'MEN')])[2]"))).build().perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.moveToElement(driver.findElement(By.xpath("(//a[contains(text(),'Socks')])[1]")));
action.click().build().perform();
Try this:
//a[normalize-space(text()) = 'Socks']
I would recommend you to not use such long xpath's and try to write xpath on your own.
Try :
//li[contains(#class,'menu_men')]//a[contains(text(),'Socks')]

How to click on a button in a Alert?

i have to automate this page using java ,and selenium.
I must click on the link which has label terms and conditions it shows up a box, and then i need to navigate it down, and click on Agree button.
I have already tried:
driver.click("//*[#id=\"field_terms\"]/td[2]/div/div/label[1]/a"); // Click on the link
it opens the box for me, but i stuck after it.
I have 2 problems:
How to scrol down the pop up?
How to click on the Agree button?
Update:
Regarding the scroll problem i used the below method which does not work:
public void scrollDown() {
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("arguments[0].scrollTop = arguments[1];","window.scrollBy(0,450)", "");
}
Try using the CSS Selector to locate the I Agree button
'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'
It worked in my system. I am not a Java person, this is the code I wrote in python for your reference
driver = Chrome()
driver.get('https://signup.insly.com/signup')
terms_and_conditions = driver.find_element_by_link_text('terms and conditions')
terms_and_conditions.click()
import time
time.sleep(2)
i_agree = driver.find_element_by_css_selector(
'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'
)
i_agree.click()
Let's start with the scroll: just use JavaScript...
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");
Or you can just scroll up to the element needed...
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", your_WebElement);
For the click 'I agree' use XPATH like this:
"//*[text()='I AGREE']" then just preform click()
Hope this helps you!
Here is the code in Java [Tested & worked].
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://signup.insly.com/signup");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.linkText("terms and conditions")).click();
//Click the I AGREE button
WebElement btnIagree = driver
.findElement(By.xpath("//button[#btnclass='primary' and contains(text(),'I agree')]"));
btnIagree.click();
//Verify the check box for T&C is checked
WebElement chkbxTnC=driver.findElement(By.xpath("//*[#id='agree_termsandconditions']/../span[#class='icon-check-empty icon-check']"));
Assert.assertTrue(chkbxTnC.isDisplayed());
The problem is, as soon as you click on the T&C link, it takes few seconds to load the page and since a wait is needed before hitting the I AGREE Button. Apparently, the I AGREE Button is enabled and clickable without the need of scrolling down, so scrolling is not needed here.

click on a button with JQuery

There is somthing wrong with the page I want to test.
My first try:
When I clicked manually on a button, then I will be forwarded normally on the next page.
When I tried to click on the same button with selenium, then I get an error page "Sorry...something gone wrong...blabla". I think this problem can only solve the developer team of the page.
By book = By.cssSelector("#button\\.buchung\\.continue");
//By book = By.cssSelector("button.buchung.continue");
//By book = By.xpath("//*[#id='button.buchung.continue']");
WebElement element= ConfigClass.driver.findElement(book);
element.click();
But I want to try a workaround:
I clicked on the same button with JQuery.
I opened my chrome console and execute the button with:
jQuery('#button\\.buchung\\.continue').click()
How can I execute this JQuery expression in my selenium code?
I tried this, but without success:
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("jQuery('#button\\.buchung\\.continue').click()");
Use $
je.executeScript("$('#button\\.buchung\\.continue').click()");
jQuery("selector") will return you a list.
I think you have to call click() on the element at index 0 (Assuming exactly one element satisfies the selector)
Code:
je.executeScript("jQuery('#button\\.buchung\\.continue')[0].click()");
You were pretty close. If the cssSelector is uniquely identifying the WebElement you can use the following code block :
By book = By.cssSelector("#button\\.buchung\\.continue");
WebElement element= ConfigClass.driver.findElement(book);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

Timed out after 10 seconds waiting for presence of element located using Selenium Wedriver

Locator for this is correct, but when running it in Selenium Webdriver, I am getting the same error .
I have used different kinds of waits like Implicit wait, Explicit wait, and Wait for Presence of element
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xp‌​ath("//div[#id = 'bs-example-navbar-collapse-1']/ul[2]/li[1]")));
driver.findElement(By.xpath("//div[#id = 'bs-example-navbar-collapse-1']/ul[2]/li[1]")).click();
Instead of using 'presenceOfElementLocated(By locator)', use 'visibilityOfElementLocated(By locator)'.
'presenceOfElementLocated(By locator)' only confirms the presence of element in DOM, it does not mean that element is visible or enable. But if you use 'visibilityOfElementLocated(By locator)', then it confirms the element is present in DOM as well as visible. If you are clicking the element you can also use 'elementToBeClickable(By locator)';
Its Shine.com , clicking on Login Link at top right hand side
You should try using By.linkText() to click on Login button as below :-
driver.get("http://www.shine.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Login"))).click();

Java selenium click element not working

I want to click the load More link in a page. My code is below.
pageUrl="http://www.foundpix.com/category/actor/bollywood-actor/"
WebDriver driver = new FirefoxDriver();
driver.get(pageUrl);
driver.manage().window().maximize();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,2500)", "");
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("json_click_handler")));
driver.findElement(By.id("json_click_handler")).click();
How can I make it click the link.
You can use below xpath to click Load More button both the times:-
driver.findElement(By.xpath("//*[#id='blocks-left']/div/div[3]/div[contains(.,'Load More')]")).click();
this button change the location after you click on it and it can be clicked twice so:
before the first click use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div")).click();
after first click,you can use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div[2]")).click();
Maybe come at it from a different angle. Do you really need to have the link clicked or do you have some Javascript function that gets called on click of the link (like window.loadMore). Can you call the function directly? Selenium is a bit annoying in the sense you can only click a visible element (I don't mean it has to be in the viewport - it just can't have a style like display:none;).

Categories