I am trying to delete every row in a table untill there are none left, only then do I want to continue with the rest of my test case. For this reason I'm using an if/else statement. As part of this statement I need to select the first row, click the delete button and then confirm my action by clicking OK. The problem is in this last action. I can find the OK via the findElement by.id but not via class and text value. Problem is that the ID is only reliable for deleting the first row as the application uses auto generated ID's. That is why I'm trying to find the OK via it's class and text value.
if(driver.findElement(By.xpath("first row of the table")) != null)
{
driver.findElement(By.xpath("first row of the table")).click(); //select row
driver.findElement(By.xpath("//*[#type='button' and text()='Delete']")).click(); //click delete button
wait.until(ExpectedConditions.elementToBeClickable(By.id("ext-gen483"))); //wait untill the OK button (id=ext-gen483) in dialog box is visible to confirm delete
/** this section is built in to find out why I cant find the OK button. First I catch and print the class and text for the OK button via the id. */
String Klasis = driver.findElement(By.id("ext-gen483")).getAttribute("Class");
System.out.println("Value of Class = "+Klasis);
String Tekstis = driver.findElement(By.id("ext-gen483")).getText();
System.out.println("Value of Text = "+Tekstis);
String xpathOK = "//*[#class='"+Klasis+"'"+" and text()='"+Tekstis+"']";
System.out.println(xpathOK);
driver.findElement(By.xpath(xpathOK)).click();
}
else
The print actions result in:
Value of Class = x-btn-text
Value of Text = OK
//*[#class=' x-btn-text' and text()='OK']
But the button cant be clicked: ElementNotVisibleException: Element is not currently visible and so may not be interacted with
However, if I use the following code to click the OK, it works just fine:
driver.findElement(By.id("ext-gen483")).click();
This is the line of code representing the OK button:
<button class=" x-btn-text" id="ext-gen483" type="button">OK</button>
As a result of a suggestion that the extra space in the class might be getting in the way I changed the code so that it no longer uses the class but the button attribute:
String Klasis = driver.findElement(By.id("ext-gen483")).getAttribute("Class");
System.out.println("Value of Class = "+Klasis);
String Tekstis = driver.findElement(By.id("ext-gen483")).getText();
System.out.println("Value of Text = "+Tekstis);
String Typeis = driver.findElement(By.id("ext-gen483")).getAttribute("Type");
System.out.println("Value of Type = "+Typeis);
String xpathOK = "//*[#type='"+Typeis+"'"+" and text()='"+Tekstis+"']";
System.out.println(xpathOK);
//driver.findElement(By.id("ext-gen483")).click();
driver.findElement(By.xpath(xpathOK)).click();
The print actions result in:
Value of Class = x-btn-text
Value of Text = OK
Value of Type = button
//*[#type='button' and text()='OK']
But it still fails on the last line with the same message:ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Edit
there are 2 buttons that comply to my findelement statement. If I then choose to click the second it works!
List<WebElement> listOfOKbut = driver.findElements(By.xpath("//*[#class=' x-btn-text' and text()='Ja']"));
System.out.println("aantal ja knoppen:"+listOfOKbut.size()); if(listOfOKbut.size() >= 2) {
listOfOKbut.get(1).click();
}
try using contains instead of "=" for the #class part:
String xpathOK = "//*[contains(#class,'x-btn-text') and text()='OK']";
To find an element more accurately use the following methods
String xpath = "//button[contains(#class,'x-btn-text') and text()='OK']"
Find the parent element of button whose class or id is constant.
xpath = "//parent//button[contains(#class,'x-btn-text') and text()='OK']"
Related
Dynamically created text-box on add button click with same id and class-name not able to send text second/third text-box.
List<WebElement> clientidtxt = driver.findElements(By.xpath("//label[contains(.,'Client ID')]/following::input[#id='CId']"));
for (WebElement webElement1 : clientidtxt)
{
if(!clientidtxt.isEmpty())
{
clientId.sendKeys(Keys.ENTER);
clientId.sendKeys(uuid);
System.out.println(webElement1.getText());
}
}
I have already send text to first text-box but not able to send to the second or third ....
Your code is kinda confusing.
You are looping through a list of web elements but inside you check to see if the list is empty... it can't be if you are looping through the list so that part can be removed.
You are using a foreach but you aren't actually using webElement1 but instead are referencing clientId which isn't declared in the code you posted.
I've updated the code with a best guess. Try this and see if it's what you are looking for.
List<WebElement> clientidtxt = driver.findElements(By.xpath("//label[contains(.,'Client ID')]/following::input[#id='CId']"));
for (WebElement webElement1 : clientidtxt)
{
webElement1.sendKeys(Keys.ENTER); // What's this for? Can this be removed?
webElement1.sendKeys(uuid);
System.out.println(webElement1.getText()); // if webElement1 is an INPUT, this needs to be webElement1.getAttribute("value") to return what the INPUT contains
}
I'm trying to click on all the search results with a loop and get the title strings from each of the results. So it would click on a result try to extract the string.
String title = null;
List <WebElement> links = driver.findElements(By.className("thumbnail"));
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
for(int i=0; i<1; i++){
links = driver.findElements(By.className("thumbnail")); // this step is must, because whenever you go to other page all store WebElements in a list will wash out
links.get(i).click();
//it opens the search result in a new tab and gains focus on that tab
WebDriverWait wait = new WebDriverWait(driver, 10);
By addItem = By.xpath("//*[#id=\"HEADING\"]");
// get the "Add Item" element
WebElement element1 = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));
wait.until(ExpectedConditions.stalenessOf(element1));
if(!driver.findElements(By.xpath("//*[#id=\"HEADING\"]")).isEmpty()) {
title = driver.findElement(By.xpath("//*[#id=\"HEADING\"]")).getText();
}
else {
System.out.println("Title is missing");
}
System.out.println(title);
driver.switchTo().window(tabs.get(0)); //Switching to first tab
}
The code is extracting the title on the first page rather than the page it clicked on. I'm also trying to extract other strings such as address, email, etc but i'm just testing this out. How do I fix this? Any help would be appreciated, thank you!
There were a few things that I changed.
I moved all your locators (and some other declarations) to the top and outside of the loop so they aren't redeclared inside the loop and so that they can be referenced by the .findElement() calls.
Since you are only using the current window handle, changed the variable type to String and just got the current window handle (instead of the collection of handles) so that you can switch back to the main tab at the end of the loop.
Moved the staleness check right after the click since that's where you need it and changed it to wait for the thumbnail that was just clicked.
Changed the XPath locator to use ID since that's all you were referencing. It's faster, shorter, and easier to read.
The second wait now waits for the collection of elements and then uses that collection to test for empty and get the text of the first in the collection.
By addItemLocator = By.id("HEADING");
By thumbnailsLocator = By.className("thumbnail");
List<WebElement> links = driver.findElements(thumbnailsLocator);
String originalTab = driver.getWindowHandle();
Set<String> tabs = driver.getWindowHandles();
WebDriverWait wait = new WebDriverWait(driver, 10);
for(int i = 0; i < links.size(); i++)
{
links = driver.findElements(thumbnailsLocator); // this step is must, because whenever you go to other page all store WebElements in a list will wash out
links.get(i).click();
// it opens the search result in a new tab and gains focus on that tab
// switch to the new window
for(String handle : driver.getWindowHandles()){
if (!handle.equals(originalTab))
{
driver.switchTo().window(handle);
break;
}
}
// get the "Add Item" element
List<WebElement> addItems = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(addItemLocator));
if(!addItems.isEmpty())
{
System.out.println(addItems.get(0).getText());
}
else
{
System.out.println("Title is missing");
}
driver.close(); // close current tab
driver.switchTo().window(originalTab); // switch to original tab
}
I have a problem to select from dropdown menu by using SelectByValue ignoring case sensitivity.
For example:
Japan
Albania
As it is seen value is Japan. However, the value which I have can be "japan" or "Japan".
I can able to select by using. However, it takes considerable amount of time if the list is huge.
// get dropdown elements
Select dropdown = new Select(findElementHelper(by));
// get elements based on options from dropdown menu
List<WebElement> myElements = dropdown.getOptions(); //because of listing takes time
// test until value of element and given value is equal
String tempValue = value.trim();
for (WebElement option : myElements) {
if (tempValue.equalsIgnoreCase(option.getAttribute("value").trim())) {
// tryClick(option,value) did not work on ie
/*if (!tryClick(option,value)){
System.out.println(value + " is not selected");
return false;
} option.click(); //worked one
break;
}
}
I have tried Select class with proper input and it works much faster than my code. Is there any way to ignore case sensitivity in selectByValue.
Thanks for help
The Selenium implementation of selectByValue() searches for the given value using xpath instead of looping through all of the options. You should be able to change the xpath search to be change everything to lowercase instead. If you know there will only be one option with the given value then you can simplify this further by removing the List and for loop.
WebElement dropdownElement = findElementHelper(by);
String tempValue = value.trim().toLowerCase();
List<WebElement> matchingValues = dropdownElement.findElements(By.xpath(
".//option[lower-case(#value) = '" + tempValue + "']"));
for(WebElement matchingValue : matchingValues)
{
/* Do what you want with the options
if (!option.isSelected()) {
option.click();
} */
}
I don't work with Selenium in Java so I'm not able to test this but it should be pretty close. Let me know if this is faster.
I am looping through a list of checkboxes upon click of a button. What I am looking to do is grab the name of the checkbox at runtime to strip out an integer value specified within the name. I am not looking to get the value nor the id. So in the strings.xml file, <string name="checkList1">Pain assessment.</string>, I am trying to get checkList1 at run time. I can get the text without a problem. Currently I am looping through the view elements with the code below:
RelativeLayout root = (RelativeLayout)findViewById(R.id.Root);
for (int i = 0; i < root.getChildCount(); i++)
{
View v1 = root.getChildAt(i);
Class c = v1.getClass();
if (c == CheckBox.class)
{
CheckBox thisBox = (CheckBox)v1;
if (thisBox.isChecked())
{
String text = (String)thisBox.ge;
DoDailyCheckListUpdate(thisBox.isChecked(),checkBoxCount);
countItemsFinished++;
}
checkBoxCount++;
}
}
What I am looking for is to somehow get the name of Checkbox thisBox. So when it loops through and hits the Pain Assessment checkbox, I want to be able to pull out checkList1. Without going as far as ripping through the strings.xml file based on the text I find to get the name, I was hoping maybe there was a simpler solution that I maybe overlooking.
Thank You in advance.
CheckBox extends from TextView so to retrieve a text from it is quite simple :
String text = thisBox.getText().toString();
http://developer.android.com/reference/android/widget/CheckBox.html
If you want to retrieve the key name of the string. I suggest you put it into the tag of the object :
thisBox.setTag(getResources().getResourceEntryName(R.string. checkList1);
Retrieve it like that :
String text = (String)thisBox.getTag();
that should do the trick.
Hi I would like to count how many times a text Ex: "VIM LIQUID MARATHI" appears on a page using selenium webdriver(java). Please help.
I have used the following to check if a text appears in the page using the following in the main class
assertEquals(true,isTextPresent("VIM LIQUID MARATHI"));
and a function to return a boolean
protected boolean isTextPresent(String text){
try{
boolean b = driver.getPageSource().contains(text);
System.out.println(b);
return b;
}
catch(Exception e){
return false;
}
}
... but do not know how to count the number of occurrences...
The problem with using getPageSource(), is there could be id's, classnames, or other parts of the code which match your String, but those don't actually appear on the page. I suggest just using getText() on the body element, which will only return the page's content, and not HTML. If I'm understanding your question correctly, I think that is more what you are looking for.
// get the text of the body element
WebElement body = driver.findElement(By.tagName("body"));
String bodyText = body.getText();
// count occurrences of the string
int count = 0;
// search for the String within the text
while (bodyText.contains("VIM LIQUID MARATHI")){
// when match is found, increment the count
count++;
// continue searching from where you left off
bodyText = bodyText.substring(bodyText.indexOf("VIM LIQUID MARATHI") + "VIM LIQUID MARATHI".length());
}
System.out.println(count);
The variable count contains the number of occurrences.
There are two different ways to do this:
int size = driver.findElements(By.xpath("//*[text()='text to match']")).size();
This will tell the driver to find all of the elements that have the text, and then output the size.
The second way is to search the HTML, like you said.
int size = driver.getPageSource().split("text to match").length-1;
This will get the page source, the split the string whenever it finds the match, then counts the number of splits it made.
You can try to execute javascript expression using webdriver:
((JavascriptExecutor)driver).executeScript("yourScript();");
If you are using jQuery on your page you can use jQuery's selectors:
((JavascriptExecutor)driver).executeScript("return jQuery([proper selector]).size()");
[proper selector] - this should be selector that will match text you are searching for.
Try
int size = driver.findElements(By.partialLinkText("VIM MARATHI")).size();