I am trying to click on a link from a dropdown list in the menu. Selenium seems to be able to find the element. But not able to click on it and giving the below exception:
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Cannot click on element
I am using IEDriver to run the code.
Below is the java code I am using to find and click on the element:
File file = new File("C:\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("url");
driver.findElement(By.xpath("//*[#id=\"Ul1\"]/li[2]/a")).click();
driver.findElement(By.xpath("//*[#id=\"Ul1\"]/li[2]/ul/li[1]/a")).click();
Below is the HTML body:
<body><ul class="sf-menu" id="Ul1"><li class = "current"><a target="bodyFrame" href="http://hostname.default.aspx">Home</a><ul></ul></li><li class = "current">Create Usage<ul><li class="current"><a target="bodyFrame" href="../SAMPLEAPPDT/Usage.htm" title="Usage Generator (SAMPLEAPP Rating)"">Usage Generator</a> <ul></ul></li><li class="current"><a target="bodyFrame" href="../NETWORKUG/network_usage/NETWORKUsageUpload.aspx?appId=1" title="NETWORK"">NETWORK</a><ul></ul></li><li class="current"><a target="bodyFrame" href="../NETWORKUG/network_usage/NETWORKUsageUpload.aspx?appId=2" title="RSS Usage Generator"">RSS</a><ul></ul></li></ul></li></ul><iframe name="bodyFrame" id="bodyFrame" src="" width="100%" frameborder="no"></iframe></body>
Please let me know what could be the issue
Assuming you are using some JavaScript code to open/close this dropdown, you might need to wait for the dropdown to open before you can select the element because it is not yet visible. Your second "click" might be too fast after the first one.
For example, you can implicitely wait for a certain
amount of time like so:
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
A better alternative would be to wait for your element to be visible like this:
WebDriverWait webDriverWait = new WebDriverWait(driver, 10);
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"Ul1\"]/li[2]/ul/li[1]/a")));
Do this after you click the first "a" element and before you try to click on the second.
Thanks for the responses.
The issue seems to be that after selenium clicks on the menu, the list shows up and disappears again. So the second findElement i was using to click on the link in the menu dropdown was not working since selenium is not able to find the element.
I was able to resolve the issue by making selenium hover over the menu and clicking on the first link in the list
Below is the code I used:
Actions action = new Actions(driver);
WebElement webelement = driver.findElement(By.xpath("//*[#id=\"Ul1\"]/li[2]/a"));
action.moveToElement(webelement).moveToElement(driver.findElement(By.xpath("//*[#id=\"Ul1\"]/li[2]/ul/li[1]/a"))).click().build().perform();
Java with Selenium Webdriver: Unable to click On the link list but I am able to print out all the link.
I also tried to click on the link using : linktext, href,JS, xpath, CSS, action.double click and click.
//this is my code
WebElement hometab=driver.findElement(By.xpath("//*[#id='new_nav']/li[1]/a"));
hometab.click();
List<WebElement> homelist1=driver.findElements(By.xpath("//ul/ul/li[1]/ul/li/a"));
int allLinks = homelist1.size();
for(int i=0;i<=allLinks;i++) {
List<WebElement> homelis=driver.findElements(By.xpath("//*[#id='main_form']/div[2]/div/ul/ul/li[1]/ul/li"));
WebElement homelis11=driver.findElement(By.xpath("//ul/ul/li[1]/ul/li[1]"));
System.out.println(homelis.get(i).getText());
WebElement element = homelis.get(i);
System.out.println(homelis.get(i));
System.out.println(homelis.get(i).getText());
homelis11.click();
element.submit();
System.out.println("Inside action class");
Actions actions = new Actions(driver);
actions.moveToElement(homelis11).click().build().perform();
System.out.println("JS click ");
//js click
JavascriptExecutor exec = (JavascriptExecutor) driver;
exec.executeScript("arguments[0].click()", homelis11);
//verify the text on that page
WebElement textq=driver.findElement(By.xpath("//h1"));
System.out.println(textq.getText()+UIActions.tab);
//back to home page with all the menu list
driver.navigate().back();
Thread.sleep(15);
Identify the select HTML element:
WebElement mySelectElement = driver.findElement(By.id("mySelect"));
Select dropdown= new Select(mySelectElement);
or pass it directly to the Select element:
dropdown = new Select(driver.findElement(By.id("mySelect")));
To select an option you can do:
All select/deselect methods will throw NoSuchElementException if no matching option elements are found.
Select by Visible Text (select all options that display text matching the argument):
dropdown.selectByVisibleText("Italy");
or
Select by Index (select the option at the given index. This is done by
examining the “index” attribute of an element, and not merely by counting):
dropdown.selectByIndex(2);
http://loadfocus.com/blog/2016/06/13/how-to-select-a-dropdown-in-selenium-webdriver-using-java/
Related
I have a scenario where I need to click Continue button.Even I gave print message after click function.The message is printing but the button is not clicking.I have tried java script executor,explicit wait(elementtobeclickable)But still its not clicking.What is the other solution.
This is what I have tried till now
By click_continue= By.xpath("//input[#id='btnWFContinue']");
if(driver.findElement(click_continue)!=null) {
waitVar.until(ExpectedConditions.elementToBeClickable(driver.findElement(click_continue)));
WebElement ele = driver.findElement(click_continue);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
log.info("Clicked on Continue!!!");
/*
WebElement element = driver.findElement(click_continue);
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
driver.findElement(click_continue).click(); */
}else {
log.info("Continue button is not present moving to next step");
}
Open the desired page in your browser and open the browser console . Execute the below code in the same
$("input[id='btnWFContinue']").click() or document.getElementById("btnWFContinue").click()
If the element is clickable , then the above javascript command must change the webpage. Use the same in your script as well. Other there is no click event associated with your element.
try placing a wait statement before your if statement. Its possible that the code executes before the elemnt is loaded and the code jumps to the else part.
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')]
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);
I'm trying to test drag and drop functionality using selenium webdriver while writing test scripts using java in eclipse.I'm unable to find the web element that I want to drag while my other friend is able to find the element with same code.
Below is the code we both have used.
WebElement draggable = driver.findElement(By.id("draggable"));
I'm using site www.way2automation.com/demo.html and the page i'm trying to find webelement on, is http://way2automation.com/way2auto_jquery/draggable.php
Please suggest something to find the webelement.
Try this below code, using Action Class.
WebElement from = driver.findElement(By.id("drag"));
WebElement to = driver.findElement(By.id("drop"));
Actions act = new Actions(driver);
act.clickAndHold(from).build().perform();
Thread.sleep(3000);
act.clickAndHold(to).moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
WebElements were not being identified even with right locators because these webElements were under an iFrame so First we need to switch to the iFrame before performing drag and drop. And then we also need to check for the availability of SourceElement and DestinationElements. Below code is working fine for this problem.
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".ui-droppable"));
builder.dragAndDrop(Sourcelocator,Destinationlocator).build().perform();
String actualText=driver.findElement(By.cssSelector("#droppable>p")).getText();
Assert.assertEquals(actualText, "Dropped!");
I'm trying to write some Selenium tests to test Pandora FMS using the Java implementation of the webdriver exported by the Selenium IDE.
The initial login part works just fine:
driver = new FirefoxDriver();
baseUrl = "http://brmew.lab.brmew.es";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl + "/pandora_console/index.php");
driver.findElement(By.id("nick")).clear();
driver.findElement(By.id("nick")).sendKeys("my");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("credentials");
driver.findElement(By.id("submit-login_button")).click();
Then, the problematic part, which is clicking a menu. I've tried to do the most simple approach:
driver.findElement(By.xpath("//ul[#id='subViews']/li[4]/a/div")).click();
But it did not work, so I tried:
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[#id='subViews']")));
myDynamicElement.click();
You can find the HTML I'm testing this with in this link (it's way too large to paste it here)
Hidden menu
Shown menu
Are you getting any exception? It seems that the element submemu item you are trying to click is invisible. If it get displayed on main menu click or mouse over you need to do that before perform click action to the element. For Example:
//Click main menu to open submenu
driver.findElement(By.xpath(".//*[#id='Views']/div")).click();
//now access submenu
driver.findElement(By.xpath(".//ul[#id='subViews']/li[4]/a")).click();
Or alternately more preferable way is:
WebElement viewsMenu = driver.findElement(By.xpath(".//*[#id='Views']/div"));
viewsMenu.click();
//or mouse over
Actions action = new Actions(webdriver);
action.moveToElement(viewsMenu).build().perform();
//now access submenu
viewsMenu.findElement(By.xpath(".//ul[#id='subViews']/li[4]/a")).click();
From ur given link, i can't find the monitoring > views > agent detail
But it seems that first u have to click on
monitoring and than wait
than click on
views and wait
than click on ur
agent detail
for mouse hover use the below code
//get element as ur wish by css or xpath or id
WebElement elem = driver.findElement(By.cssSelector("ur locator"));
Actions builder = new Actions(driver);
builder.moveToElement(elem).perform();
makeWait(5);
than after showing the element, than again get next element and use the code for hover.