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.
Related
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')]
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.
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/
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;).
There is an invisible element on my HTML page which becomes visible when a mouse hover is done on the element. What I Have to do is
Hover over the element
Click on the element (it will display 4 options)
Click on one of the options
I am using Java API for selenium web driver and following is what I have been trying
Actions builder = new Actions(driver);
builder.moveToElement(MainMenuBTN).click().build().perform();
subMenuBTN.click();
MainMenuBTN = element that becomes visible when you hover the mouse
over it
subMenuBTN = element that is being chosen out of the menu options
that are displayed
What is happening is, the click() on MainMenuBTN is generating ElementNotVisible exception.
I tried following to avoid this, but did not work.
Actions builder = new Actions(driver);
builder.moveToElement(mainMenuBTN).build().perform();
builder.click();
subMenuBTN.click();
A Note : mainMenuBTN and subMenuBTN are WebElements generated by
driver.findElement(By.xpath("xpath_string"))
Am I missing anything? Help appreciated !
Well, after going through your questions numerous times and changing my answers many times I will go with -
Issue - what I got from the original code -
You need to move the cursor to the mainMenuBTN (which is visible not the element that becomes visible when you hover the mouse over it ) and subMenuBTN is then displayed which you need to click.
The only edit to your original code as per me will be adding a statement to move the cursor to your subMenuBTN before you click it. This way works fine for me when I need to click sub menu item.
Actions builder = new Actions(driver);
builder.moveToElement(mainMenuBTN).build().perform();
builder.moveToElement(subMenuBTN).build().perform();
subMenuBTN.click();
Please let me know if this is the case.
using javascript executor like
((JavascriptExecutor) webdriver).executeScript("document.getElementById('btn').click();");
Your Actions builder looks slightly wrong to me. Here is a example I use:
public static void mouseClickByLocator( String locator ) {
WebElement el = driver.findElement( By.cssSelector( locator ) );
Actions builder = new Actions(driver);
builder.moveToElement( el ).click( el );
builder.perform();
}
Actions builder = new Actions(driver);
builder.MoveToElement(menu).MoveToElement(submenu).Click().Perform();
It works under Chrome, but doesn't work in FF
You could try this:
Get your WebElement by its xpath.
Hover element.
Get your WebElement by its xpath again.
Click it.
It's because of element's id is changing when you hover over it and you should find it again.
Actions builder = new Actions(driver);
WebElement mainMenuBTN = getWebEl("xpath_string",5);
builder.moveToElement(mainMenuBTN).perform();
mainMenuBTN = getWebEl("xpath_string",5);
builder.click(mainMenuBTN);
I use this method to ipmlement controlled explicit wait into my elements' instantiation.
protected WebElement getWebEl(String xpath, int waitSeconds) {
wait = new WebDriverWait(driver, waitSeconds);
return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
}
My case we had a table of rows, if mouse over on the row, one of the column(td) should display some 4 icons, we should click on it.
Action action=new Action(driver);
action.moveToElement(hoverElt).clickAndHold().build().perform();
It worked for me.
moveToELement() move your control to the element
clickAndHold() clicks and holds the hovered element, so that its easy for us to do operation on visible elements.