Trying to click on service by using xpath
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/header/section/div[2]/div/div/ul/li[2]/a")));
driver.findElement(By.xpath("/html/body/header/section/div[2]/div/div/ul/li[2]/a")).click();
but element is not getting selected/clicked.please help
try as follows (if the element is not inside a frame):
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#id='menu1']/span[text()='Service']")));
driver.findElement(By.xpath("//a[#id='menu1']/span[text()='Service']")).click();
If above code does not work. then it is most probably the element is inside a frame.
If the element is inside a frame (the element is a child of an iframe tag), then first switch to the element and find the element.
My detailed answer about switching b/w frames here,
in selenium web driver how to choose the correct iframe
driver.findElement(By.xpath("//*[#id='menu1']"))
try to use this. else
driver.findElement(By.id("menu1"))
Related
I have an application created in ReactJS which also contains some EXTJS/jsp iframes inside it. Now when I try to click some anchor tag element (Hyperlink) using selenium click method it wasn't working. After some investigations, I found that the click is performed slightly above the element boundary. The selenium is able to identify the element using xpath but is unable to click the link correctly. Also tried using the action but it didn't work. Using offset is not an option as it relies too much on the screens resolution.
Any help is highly appreciated.
maybe you can set the width and height of the page to become larger. Some elements overlaps if the page does not support smaller screens.
If your locator is inside the IFrame, you will need to do a switch to that IFrame and then perform the click action, and after you need to switch out from that IFrame:
driver.switchTo().frame("your_iframe_name");
driver.findElement(By.xpath("your_locator")).click();
After use one of these to switch back:
driver.switchTo().parentFrame();
driver.switchTo().defaultContent();
Also, there is another way to click on a element located inside an IFrame, by executing a js script.
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.xpath("your_locator"));
I am trying to navigate through all the available pages in a website, at the beginning after scrolling to the second page I was getting exception that there is no such element on the web page, then I realize, at some point the css selector is changed in the website. Sometimes it is like that WebElement nextButton = driver.findElement(By.cssSelector("#pagination-head>a>div>span.hidden-xs.hidden-sm")); and sometimes WebElement nextButton = driver.findElement(By.cssSelector("#pagination-head > a:nth-child(3) > div > span.hidden-xs.hidden-sm")); My question is how I can handle this in selenium, I checked whether there is a method to check somehow whether a given webelement exist or not but I could no find any :/
You could use this cssSelector for both paths:
WebElement nextButton = driver.findElement(By.cssSelector("#pagination-head span.hidden-xs.hidden-sm"));
If its only these 2 options, you could work with a workaround if/else statement.
You could create a boolean checking if element can be found by your first findElement. If true, use your first code, else, use second code.
You can use if-else block if you are sure that the next button is identified only by those two locators. If in case on the third page the locator changes, your if-else block will not work. So, better approach is to go for xpath to identify the next button. You can use below sample code to locate your element
WebElement nextButton = driver.findElement(By.xpath(".//*[contains(text(),'(text on your next button)')]"));
nextButton.click();
Please replace "(text on your next button)" with the text displayed on your next button. The above code will perform the click operation on the element containing the text which you pass.
Hope this is helpful.
I'm using Selenium to do Java test.
I know that i can check if an element is enable or displayed with this :
isDisplayed
isEnabled
Is it possible to check if an element is not visible, in the case that this element is under another ? For example, if a div is under another one.
With that i want to check GUI element. For example if a button move under element, etc ...
Any ideas ?
thanks for helping _
selenium.isElementPresent() or selenium.isVisible()
those may help you.
isElementPresent() - This method basically tests if the element we are looking for is present somewhere on the page.
isVisible() - looks for display: none style tag - this might throw a null pointer if we aren't careful...thus to see if an element is visible first check if the element is present using isElementPresent() method. Then try checking if the element is visible!
I think constructing your locator like this might do it.
By TOP = By.xpath(".//div");
By UNDER = By.xpath("..//..//div");
By elementUNDERtheTOP = new ByChained(TOP, UNDER);
This is the equivilant of:
driver.findElement(TOP).findElement(UNDER);
This is possible to do with XPath but I don't think you could do this with a CSS locator because a CSS locator would not be able to traverse up the DOM tree to parent elements.
Lets say you want to click an element, but that element is "behind"/"below" (aka 'obstructed' by anlother element), Selenium will actually throw a ElementClickIntercepted exception. As part of the exception message, it also returns which point you tried to click and which other element (tag, class) did intercept the click. By simply attemping to click an element and specifically catching this type of exception, you get the information you're looking for, if you catch such an exception => element not clickable (at the given coordinates - in most cases the default click point (or if you use actions: the offset you define)), if no exception => element is clickable.
The solution for Python is here.
It should be easy to convert it to Java.
Click on the element for the below HTML code is not working.
a class="add" href="/travelPlan_revamp/addTravel.htm?travel_type=D">Add</a>
I'm using the next Xpath: html/body/div[1]/div[2]/div/a
But No Such Element Found exception is displayed.
First make sure the web element is not inside iframe.
If yes, Then you need to switch to iframe first and then try clicking on element.
Also, try below xpath, in case you have captured wrong xpath:-
//a[#class='add' and .='Add']
An absolute xpath always starts with a forward slash '/'. I think you have used an absolute xpath and either you have missed'/' or you quoted it wrong here. Please verify it. Normally absolute xpath will look like /html/body/form/div[3]/div[2]/div[1]/div[2]/input
Try this xpath:
//a[contains(text(),'Add')];
Sorry guys the the thing is it was in Iframe. Now i have found the solution by switching to the iframe and then clicking an event.
driver.switchTo().frame("rightMenu");
driver.findElement(By.xpath("html/body/div[1]/div[2]/div/a")).click();
Hard to tell if your XPATH expression is right without the HTML.
Your XPATH expression tell:
Take a "a" elements
In the 2nd div element
In the first div element
In the body element
That is in the html element
So checks there is a "a" element that check the Xpath expression.
//a[#class='add' and text()='Add']
Try that one.
Through which method are you looking for the element?
#FindBy (how = How.XPATH)
or
driver.findElement(By.xpath());
Sometimes the #FindBy notation doesn't like working with Xpaths properly.
Try with this below xpath
//a[contains(text(),'Add')]
Is there a trick to selecting page elements one after the other using java webdriver? I am trying to set values in a form which is inside an iframe.
I first switch the iframe and can access and change the values in the first dropdown. I can create a WebElement from the other pages but I cannot interact with them at all.
The code below allows me to change the value of the first drop down.
driver.switchTo().frame(0)
WebElement fromList = driver.findElement(By.id("foo"));
r.selectItemByText(fromList, "var");
I cannot access any of the elements after that. I can find them all with no error but I cant do anything with them as it says they have no data.
WebElement fromList = driver.findElement(By.id("bar"));
r.selectItemByText(fromList, "foo");**
The second line returns this error:
NoSuchElementException: Cannot locate element with value 'foo'
If I remove it I get no error so the code is seeing the element just not the values of it.
If the elements are on a different frame, you must switch back to the default frame, then back down again.
Something like:
driver.switchTo().defaultContent();
Reason is because once you switch to a frame, if you switch to another frame, the search will be only of the child frames to that original frame. So you must switch back to the top frame, and back down again.