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
}
Related
I have a for loop that constantly loops back a website to fill out and submit a search box with the next string from a list.
For example, if the list contains [abcd, efgh, ijkl....], the first loop would send abcd, the second loop would send efgh and etc.
Currently the loop sends the first string correctly, but on every loop afterwards, the string sent is added to the previous string. So instead of efgh on the second loop, it is abcdefgh.
How do I clear the keys so that I can just send the individual string? Here's what I have right now:
for(String value : List){
driver.get(Link);
Actions actions = new Actions(driver);
WebElement input_field = driver.findElement(By.id("txtBoxSearch"));
actions.moveToElement(input_field);
actions.click();
actions.sendKeys(value);
actions.build().perform();
WebElement submit_key = driver.findElement(By.xpath("//button[contains(#title, 'Search')]"));
actions.moveToElement(submit_key);
actions.click();
actions.build().perform();
}
You need to clear the content of textbox before write value to it
for(String value : List){
driver.get(Link);
Actions actions = new Actions(driver);
WebElement input_field = driver.findElement(By.id("txtBoxSearch"));
input_field.clear();
actions.moveToElement(input_field);
actions.click();
actions.sendKeys(value);
actions.build().perform();
WebElement submit_key = driver.findElement(By.xpath("//button[contains(#title, 'Search')]"));
actions.moveToElement(submit_key);
actions.click();
actions.build().perform();
}
Here is the Answer to your Question:
Once you call click() method on the on the desired element through action class instance, next use the action class instance to clickAndHold, send CONTROL A and then send the new text DebanjanB to the form control to overwrite the previous text as follows:
for(String value : List){
driver.get(Link);
Actions actions = new Actions(driver);
WebElement input_field = driver.findElement(By.id("txtBoxSearch"));
actions.moveToElement(input_field);
actions.click();
actions.clickAndHold(input_field).sendKeys(Keys.chord(Keys.CONTROL, "a"), "DebanjanB").build().perform();
WebElement submit_key = driver.findElement(By.xpath("//button[contains(#title, 'Search')]"));
actions.moveToElement(submit_key);
actions.click();
actions.build().perform();
}
Let me know if this Answers your Question.
I want to click on an element inside a list and go to different page. In this page I m taking a string. Then I go back and do the same for others. But after one iteration my code can't find the second element and shuts down the browser. Am I using the navigator wrong?
Here is my code:
public MainPage ControlSorting() {
List <WebElement> listItems=driver.findElement(RESULTCONT).findElements(MEDIA);
String[] strImdb = new String[listItems.size()];
int l = 0;
for (WebElement ele1 : listItems) {
ele1.click();
WebElement element = getElementBy(ABOUTIMDB);
String a= element.getAttribute("ng-genre-action");
String[] parts = a.split(",");
strImdb[l]=parts[1];
l++;
driver.navigate().back();
}
return this;
}
After going back you have to re identify the object. Please add the following code inside for loop at the first line of your code.
listItems=driver.findElement(RESULTCONT).findElements(MEDIA);
This should work. Please try and let me know.
I am facing problem while checking the clickable web element.
So i have to check the alphabetic series and some of the alphabet are clickable and some are not clickable.
i used for loop for it starting with xpath of alphabet 'A'
and going in loop till alphabet 'Z'.
but as soon as xpath of alphabet A is click & pass it goes to alphabet 'B'
which is not clickable and due to this whole script is getting fail.
here is the code
for(int j=3; j<=26;j++) {
String T1 =".//*[#id='twctvEl']/div/div/div[1]/ul/li[";
String T2 = "]/a";
String T12 = T1+j+T2;
chrome.findElement(By.xpath(T12)).click();
String alpha =chrome.findElement(By.xpath(T12)).getText();
System.out.println("checking the alphabet"+alpha);
}
Please advice here
NOTE: In the series of alpha bet from A-Z only B,Q,S,X,Y,Z are not clickable rest all are clickable.
You can add waits before element to become clickable:
for(int j=3; j<=26;j++)
{
String T1 =".//*[#id='twctvEl']/div/div/div[1]/ul/li[";
String T2 = "]/a";
String T12 = T1+j+T2;
WebElement el = chrome.findElement(By.xpath(T12));
WebDriverWait wait = new WebDriverWait(driver, timeout);
WebElement el= wait.until(ExpectedConditions.elementToBeClickable(element));
el.click();
String alpha =el.getText();
System.out.println("checking the alphabet"+alpha);
}
You can check if the element is visible and enabled before clicking on it
WebElement letter = chrome.findElement(By.xpath(T12));
if (letter.isDisplayed() && letter.isEnabled()) {
letter.click();
}
Well, it seems that when you click the element "A", it take you to another page or context and for this reason, during next iteration chrome driver is not able to find your Element "B" using xPath.
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']"
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.