I have a problem, I have a form (angularJS) and this form has 10 textarea, right now I have 10 times, sendkes action, which makes code ugly :(
How can I find all mine textareas and pass a value only once?
Right now I have something like this:
List<WebElement> allFormElements= driver.findElements(By.xpath("myPath"));
allFormElements.size();
System.out.println("Lets count all forms" +allFormElements);
for (WebElement item : allFormElements) {
if (item.getTagName().equals("textarea"))
item.sendKeys("testing values");
}
Thakns
This solution worked for me fine!
List<WebElement> allInputFields = driver.findElements(By.cssSelector("cm-edit-request-form dx-text-area textarea"));
if(allInputFields.size()!=0){
for(WebElement allInputFieldsElement:allInputFields){
if(allInputFieldsElement.isEnabled()) {
wait.until(ExpectedConditions.elementToBeClickable((allInputFieldsElement)));
allInputFieldsElement.sendKeys("Automation text" + uuid);
}
}
Related
I am trying to implement some code using Selenium Webdriver using Java.
Basically, I have a website with a text box. Once user enter the first letter, based on that a value will be displayed(using AJAX). I need to select the particular value, which i mentioned in send keys .
WebElement fromCity = driver.findElement(By.id("pickUpLocation"));
fromCity.sendKeys("A Ma Temple / 媽閣");
Thread.sleep(2000);
WebElement ajaxContainer1 = driver.findElement(By.className("txt-box ng-touched ng-dirty ng-valid"));
WebElement ajaxHolder1 = ajaxContainer1.findElement(By.tagName("ul"));
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
for (WebElement value1 : ajaxValues1) {
if (value1.getText().equals("A Ma Temple ")) {
((WebElement)ajaxValues1).click();
break;
}
}
After you send keys.your Ajax value should be retrieved in a box related to you keyword search.You need to get the complete box.and fetch each one as you have done in for loop .get the text and compare it with your expected text and click where this condition is true.
What is that line for before thread.sleep()
I think u can try selecting through index. It should be like this
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
Select dropdown= new Select(ajaxValues1);
dropdown.selectByIndex(0);
dropdown.selectByIndex(1);
dropdown.selectByIndex(2);
0 represents the first element in the dropdown. Based on index number of that element, feed the corresponding number in selectByIndex(0)
Let me know if this helps. Thanks
Screenshot
I have to test a question-answer module where user will enter answers through check box. There are total 5 j-query tabs with some questions. and it will move to other tab if answers are given and clicked "Save and next" button. I am going following way but it is not selecting every check box. because some check boxes are not displayed. Can someone suggest better way to select each and every checkbox?
List<WebElement> chkbx = driver.findElements(By.xpath("//input[#type='checkbox']"));
int j=1;
while(j<5){
for(int i = 0;i<chkbx.size();i++){
if(chkbx.get(i).isDisplayed()){
chkbx.get(i).click();
}
}
driver.findElement(By.xpath("x-path of save button")).click();
j++;
}
Try below code to get all your checkboxes and perform the click operation on the displayed checkboxes :
List<WebElement> chkbx =driver.findElements(By.xpath("//input[#type='checkbox']"));
for(WebElement e:chkbx)
{
if(e.isDisplayed())
{
e.click();
Thread.sleep(1500);
}
}
I want to print and then click on list of all items like home top stories, latest news, opinion etc each and every category as you can see in image but did get success please help..
List<WebElement> list=driver.findElements(By.id("com.readwhere.whitelabel.prabhatkhabar:id/left_drawer_list"));
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i).getText()+"\n");
}
Most probably you are trying to find the list with wrong id. If in this window only category names are present as text, try find with texts.
After trying findElements with valid id, you need to click each category and then return to window containing the list again. Try this way:
// Trying to find the list with texts
List<WebElement> list = driver.findElements(By.className("android.widget.TextView"));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getText() + "\n");
list.get(i).click(); // clicking on each category
// navigate back to previous window
}
For navigating back, you can use this code:
driver.navigate().back();
try with the below code:
List<WebElement> listForSize =driver.findElements(By.id("com.readwhere.whitelabel.prabhatkhabar:id/left_drawer_list"));
int size = listForSize.size();
for (int i=0; i< size;i++)
{
//this is taken again because you are navigate back again
List<WebElement> list = driver.findElements(By.id("com.readwhere.whitelabel.prabhatkhabar:id/left_drawer_list"));
System.out.println(list.get(i).getText() + "\n");
list.get(i).click();
driver.navigate().back();
Thread.sleep(2000);//avoid this kind of waiting. wait using until
}
let me know if any error occurs.
I am trying to manipulate this blocks of codes:
List<WebElement> more = driver.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button"));
if(more.size()!=0){
driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button")).click();
}else {
WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
System.out.println("Total Number of TR: " + list.size());
}
What Im trying to do hopefully is that before I execute what is in the IF statement I want to loop that everytime it sees the element /html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button it will be click and if it is not available then ill execute this:
WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
System.out.println("Total Number of TR: " + list.size());
For further info what I am trying to do here is this.
I am on a listview for a specific module, and then at the button there is a "Click here for more records" -> its XPath is /html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button
I want that if i am in the listview and that button is present as mentioned above -I want to click it. And if in the listview there is no button "Click here for more records" (for example the records is composed of 5records only so there is no pagination clearly) I want to execute a blocks of code.
What I understood from your query is that you want to loop your IF condition statement multiple times until your else condition satisfies. For this, you can try following code:
(1) try{
(2) String xpathVAL="/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button";
(3) int i=1;
(4) while(i!=0){
(5) if(driver.findElements(By.xpath(xpathVAL)).size() != 0)
(6) {
(7) driver.findElement(By.xpath(xpathVAL).click();
(8) Thread.sleep(2000);
(9) }
(10) else{
(11) Thread.sleep(2000);
(12) WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
(13) List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
(14) LOGGER.debug("Total Number of TR: " + list.size());
(15) i=0;
(16) }
(17) }
(18) }catch(Exception e){
(19) LOGGER.debug("Exception Caught");
(20) WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
}
I think your requirement would be to keep click a button until some window appears and the button disappears. What I suggest for this is, apply "Thread.sleep(//some 3-4 seconds//)". However, it is poor style of coding and using forceful thread is not suggested by many standards. You can put some other implicit wait there. Also, if your element is not found after certain period of time, you can apply break condition after certain time interval, else it will go to infinite loop. If you are having trouble writing the code, let me know and I will help you in that.
EDIT
Try following points to remove element not present in cache:-
Use Explicit wait(Thread.sleep) at line 8 and line 11
Change your xpath from position based to some id/name/class type. Changing it to NAME based gets more clearer picture of the element even if its position is changed.
Again re-initialize the element if exception is caught at line 20.
I'm testing drop down list values in Selenium. I am working in Java.
A drop down list has 40 values. I want my code to get all the values in the DDL and compare it to the ones they should be.
My code as it stands will fail if the values in the DDL do not match the hardcoded values I expect. It prints a message which says values do not match. However it doesnt tell me which value has not matched.
What is the best way to achieve this?
I am using selenium.getSelectOptions to get the DDL values. Then using if statement to check if values are same, if not then fail. But it doesnt tell me which value is not the same.
Well without the code is hard to help you without the code. I will try to outline a little help how do I approach this:
public void chooseProduct(String product){
List<WebElement> Options = new ArrayList<WebElement>();
productChoooser = driver.findElement(By.id("id_of_the_selectbox"));
productChoooser.click();
Select select = new Select(productChoooser);
Options = select.getOptions();
for (WebElement option:Options){
if(option.getText().equals(product)){
option.click();
}
}
The above code select one item from the dropdown. You can easily modify it bymodyfying the if statement in this, say:
boolean foundIt = false;
for (WebElement option:Options){
if(option.getText().equals(product)){
System.out.println("The product " + product + "was found in the drop down list");
foundIt = true;
}
}
if (!foundit){
System.out.println("The product " + product + "was NOT found in the drop down list");
}
}
please recheck it after me, I am writing the code directly to the answer window