Java Selenium button click - java

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.

Related

Selenium Click method performs click slightly outside the element boundary

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"));

Get last added element of specific class to DOM via xpath

My site generates an arbitrary number of popups (please don't judge my monitization strategy). Each popup is a complex div that I eventually want reference to via xpath, but I specifically want the last one that was added to the DOM, since that will be the one that's on top.
So I tried this
//*[#class="popupContent"][last()]
I know I could try this
(//*[#class="popupContent"])[2]
but I have no idea how many of these popups there are.
But in the case of having multiple popups on screen at once, I get a reference to the first one (I'm not sure if this is reliably the case or not). Each of these popups has a popupContainer, but for the sake of this question, it is off limits.
Given that there are multiple elements with this class, how can I get the one that is on top (and thus interactive)?
These things aren't siblings, children, or parents of each other. Also, I have no idea how many exist on the page at any given time. I also do not have any control over the content or structure of the popups beyond that I know the class for one of their internal components (popupContent).
If this is not possible, please explain why.
I'm looking for a solution that will be compatible in a Java Selenium testing environment. No jquery please.
According to this question: Do WebDriver findElements retain the Table rows order on its retrieval findElements guarantees order, so in a Selenium environment, I could get the list from that and retrieve the last item.
List<WebElement> popups = findElements(By.classname("popup"));
WebElement activePopup = popups.get(popups.size - 1);
Xpath (//*[#class="popupContent"])[last()] and code below should give you same result - last element in the DOM:
List<WebElement> popups = driver.findElements(By.className("popup"));
WebElement activePopup = popups.get(popups.size - 1);
If last element not the top one try to get focused element:
WebElement focused = driver.switchTo().activeElement();
if (focused.getAttribute("class").equals("popupContent"))
//my active/interactive popup
You can check if last one is active now:
WebElement lastOne = driver.findElement(By.xpath("(//*[#class="popupContent"])[last()]"));
if (lastOne.equals(driver.switchTo().activeElement()))
//last one is active
Another approach could be to look for the popup with the highest z-index value:
private WebElement getTopPopUp() {
List<WebElement> allPopUps = driver.findElements(By.classname("popup"));
WebElement topPopUp = allPopUs.get(0);
for(WebElement popUp : allPopUs) {
if(Integer.parseInt(popUp.getCssValue("z-index")) > Integer.parseInt(topPopUp.getCssValue("z-index"))) {
topPopUp = popUp;
}
}
return topPopUp;
}

how i can Search word in xpath and return the wanted WebElement?

I'm using java selenium to check a site.
I want to check if a button is exist in every page (there are a lot pages that have that button named Bar).
I'v tried to find that element in many ways, just the xpath have been worked.
by the xpath I can just check specific button.
This is my code :
WebElement BarChart = (WebElement) driver.findElements(By.xpath("//html/body/div/div/ng-view/div/div[2][contains(text(), 'Bar')]"));
This is not work.
How I can find this element(Bar button) by the word "Bar" ?
To find your button on all of pages you should use relative XPath instead of absolute as HTML tree could be different on each page. So try this one:
//button[contains(text(), "Bar")]

Appium Finding Wrong Element when Finding With xPath

I am working on a development script using Selenium and Appium and I'm running into the issue of the wrong element being picked up by the Selenium Locator.
Essentially, I want to click a button that has no ID assigned to it; so the only thing I have left to identify it by is its text.
public Element button1(){
By locator = By.xpath("//android.widget.TextView[#text='button1']");
return new xElement(driver.findElement(locator), locator);
}
This is my my locator method to get the button1 object. By the way, no other button on the screen has text anywhere close to button1's text. The method click called on the button has the format:
public void clickBtn1(){
button1().click();
}
The button being clicked essentially has the text "wheelbarrow". This is just to clarify that the button being pressed has text no where close to button1's value.
I have used UI automator multiple times to confirm button1's actual text value. The weird thing is the script works occasionally, so I'm not sure what the issue is.
I have also tried a "wait for enabled" method to account for race conditions.
Try using the Appium inspector to search for your button. You can type in the xpath and search for the element to see what it finds. The other nice thing about the inspector is you can see how the native control attributes map to the Appium attributes. 'text' may not be the attribute you actually want. Also, have you tried searching on properties on the Button itself (instead of TextView)?
If it's working occasional, First try to use different element other than xpath. Second, try to give some sleep command before you perform that action like
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(ByLocator(locator)));

Xpath is found even though web object not visible

I am automating a code using selenium 2.0. I select one (or several) user(s) from a list. Then I click on an add button which makes the user name(s) visible on a grid. Each user will have a valid Xpath when visible on the grid. However, even after erasing all user names from the grid which actually disappears if there is no user names displayed, the Xpath still does not return null. I am using Xpath to check if it returns null when the object (user name) is not visible, but it does not work as expected. Is there any other way to solve my problem? I am pretty new with Selenium. I am using selenium 2.0. Bellow is a section of my code. Your help will be very appreciated.
//Check if user is present on the grid
By checkuser = By.xpath( ".//*[#id='sharing_list']/tbody/tr/td[1]/span");
//if the grid is not empty, which means the grid is visible...
if(null!=checkuser) //where the problem is!!
{
//Click the button to erase the names in the grid, then the grid desapears
webDriver.findElement(By.xpath("//*[#id='sharing_list']/tbody/tr/td[4]/span")).click();
Thread.sleep(2000);
//more code
//............
}
I can see two things happening here:
First is that your XPath is generic enough that it is selecting some other element that isn't a user. To see if this is the case, then in Chrome, go to the page and do the necessary actions to get it in the state you want. Next, press Ctrl-Shift-J, click on Console, and type in $x("//*[#id='sharing_list']/tbody/tr/td[4]/span"). Chrome will then show you which element your selector is selecting.
Your task then, is to identify if its selecting some other element, or whether the element is just not visible. It is definitely possible to have an element on a page, but not visible, and WebDriver WILL select invisible elements (unless you are doing By.linkText()). If you want to check to see if an element is visible do a element.isDisplayed().

Categories

Resources