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);
}
}
Related
Site : https://www.myntra.com/men-tshirts
`WebElement brand = driver.findElement(By.xpath("//div[#class='brand-more']"));
brand.click();
WebElement select_brand = driver.findElement(By.xpath("//input[#placeholder='Search brand']"));
select_brand.sendKeys("H");
List<WebElement> select_HRX = driver.findElements(By.xpath("//label[#class=' common-customCheckbox']"));
for(int i=0; i<select_HRX.size(); i++) {
String brandText = select_HRX.get(i).getText();
// String[] arr = brandText.split("\\(");
String[] arr1 = brandText.split(Pattern.quote("("));
System.out.println(arr1[0]);
if(arr1[0].equalsIgnoreCase("HRX by Hrithik Roshan") && arr1[0].equalsIgnoreCase("HERE&NOW")) {
select_HRX.get(i).click();
Thread.sleep(2000);
break;
}
}`
// trying to click 2 checkboxes based on condition not able to do any suggestions ??
Here
arr1[0].equalsIgnoreCase("HRX by Hrithik Roshan") && arr1[0].equalsIgnoreCase("HERE&NOW")
You should change from && to || i.e. from logical AND to logical OR operator since each of 2 desired elements you want to click will contain first OR second text, not BOTH of them.
In case you know the anount of checkboxes you want to select you can add counter, like the below:
counter = 0;
if(arr1[0].equalsIgnoreCase("HRX by Hrithik Roshan") || arr1[0].equalsIgnoreCase("HERE&NOW")) {
counter++;
select_HRX.get(i).click();
Thread.sleep(1000);
if(counter>1){
break;
}
}
There's a better, easier, and faster way to do all of this without loops, etc. You need to build your locators using the brand names you are looking for instead of grabbing all elements and then looping to look for a match. It's also a good practice to wait for elements to be in the correct state before interacting with them to avoid unexpected exceptions, etc.
The basic flow is...
Navigate to the page
Click the Brand search icon
Enter Brand #1
Click the checkbox next to Brand #1
Wait for Brand #1 to show up as filtered on the page (this is important)
Clear the Brand search box
Enter Brand #2
Click the checkbox next to Brand #2
Wait for Brand #2 to show up as filtered on the page (this is important)
Now both of the desired brands are filtered and the script can continue.
The code
String url = "https://www.myntra.com/men-tshirts";
driver.get(url);
WebDriverWait wait = new WebDriverWait(driver, 10);
// click Brand search icon
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.filter-search-filterSearchBox"))).click();
// get the Brand search box
WebElement search = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.filter-search-inputBox")));
// search for the first brand
search.sendKeys("HRX by Hrithik Roshan");
// click the brand checkbox
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[text()='HRX by Hrithik Roshan']"))).click();
// wait for the brand to show up as filtered
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[#class='filter-summary-filterList'][contains(.,'HRX by Hrithik Roshan')]")));
// clear the search box
search.clear();
// search for the second brand
search.sendKeys("HERE&NOW");
// click the brand checkbox
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[text()='HERE&NOW']"))).click();
// wait for the brand to show up as filtered
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[#class='filter-summary-filterList'][contains(.,'HERE&NOW')]")));
...
I am using a for-loop for selecting multiple checkboxes. It selects the
checkboxes, but if I select index value 1 then I run the code, it selects 1 value but jumps 2 value and selects 3 checkboxes.
#FindBy(xpath="//li[#class='px-2']")
List <WebElement> listofitems;
for(int i=1; i<=5; i++){
listofitems.get(i).click();
System.out.println(i);
}
Use the below code
for(int i=0; i<=5; i++){
listofitems.get(i).click();
System.out.println(i);
}
If you would like to click multiple checkbox. Then try iterating between the elements and perform the click option. If you have any text in your element identifier then use something like this
List<WebElement> checkboxes = driver.FindElements(By.XPath("//li[#class='px-2']"));
foreach(WebElement chkBox in checkboxes)
{
if(chkBox.Text.Contains("checkBoxName"))
{
chkBox.Click();
}
}`
else if you want to click all the checkbox, then simply use without the if condition
foreach(WebElement chkBox in checkboxes)
{
chkBox.Click();
}
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);
}
}
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 working on a dynamic page where IDs, Xpath and name attributes changes from time to time.
In order to make my scripts stable. I want to randomly click on 5 buttons on the page and then send a random amount between 1-100 to the input box available and then finally click the Submit button(SCOMMETTI) to place a bet. The link to the page is below:
http://sports.williamhill.it/bet_ita/it/betting/y/5/Calcio.html
The script below is failing because the events keep changing at the backend:
enter code here
driver.findElement(By.xpath("//*[#id='tup_selection570167price']")).click();
driver.findElement(By.xpath("//*[#id='slip_sgl_stake570167L']")).sendKeys("5");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='tup_selection570176price']")).click();
driver.findElement(By.xpath("//*[#id='slip_sgl_stake570176L']")).sendKeys("10");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='tup_selection570179price']")).click();
driver.findElement(By.xpath("//*[#id='slip_sgl_stake570179L']")).sendKeys("4");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='tup_selection570191price']")).click();
driver.findElement(By.xpath("//*[#id='slip_sgl_stake570191L']")).sendKeys("7");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='tup_selection570200price']")).click();
driver.findElement(By.xpath("//*[#id='slip_sgl_stake570200L']")).sendKeys("100");
Thread.sleep(1000);
System.out.println("Placing amount on Bet");
If your IDs, Xpath and name attributes changes from time to time, you should store in an array and with a loop you can click on them. like this
ArrayList<WebElement> input_type = (ArrayList<WebElement>)
driver.findElements(By.tagName("input"));
for (int i = 0; i < input_type.size(); i++) {
for(WebElement type : input_type)
{
if(type.getAttribute("type").equals("button")){
type.click();
type.submit();
}
Enjoy!