Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 25 days ago.
Improve this question
enter image description here
I want to do a rondom click with selenium, but I can't. I searched but couldn't find it. I want to choose the sizes in the photo randomly. Because the sizes may be out of stock and my test will fail.
I ask for your support. I tried with list methods and I didn't.
As you have not shared the HTML structure, can't suggest you on the actual code, however see the algorithm below:
Solution1: Assuming the dropdown has select tag:
Get the dropdown values into a Select object, call it dropdownValues
Select dropdownValues= new Select(driver.findElement(By.name("name of the element")));
Create an instance(object) of Random class and generate a random number in the dropdown index range
Select the random index from the dropdownValues
dropdownValues.selectByIndex(randomElement);
Solution2: Assuming the dropdown does not have select tag:
Get the dropdown values into a List, example below
List<WebElement> dropdownValues = driver.findElements(By.xpath("enter xpath here"));
Create an instance(object) of Random class like below
Random rand = new Random();
Select the random index from the List dropdownValues
dropdownValues.get(rand.nextInt(dropdownValues .size()));
2 approach for this.
1.
WebElement InputBox = driver.findElement(by.xpath("xpath of input box"));`
InputBox.click();
WebElement DropDownAttribute = driver.findElement(by.xpath("//*[text()='Xs']"));
DropDownAttribute.click();
2.With Loop
List<WebElement> list = driver.findElements(by.xapth("Xpath of all the dropdown entity"));
for(WebElement ele:list){
if(ele.getText().equals("Xs")){
ele.click();
}
}
The easiest way to do this is to count the dropdown selections and then generate a random number between 1 and the max number of selections. NOTE: You don't want to include 0 because of the "Size" label inside the dropdown.
Random random = new Random();
By selectLocator = By.id("someId"); // the locator for the Size dropdown
Select dropdown = new Select(driver.findElement(selectLocator)); // get the Size dropdown element and convert it to a Select element to make it easier to work with
Integer size = dropdown.getOptions().size() + 1; // get the number of OPTIONs and add 1 to avoid selecting the "Size" OPTION
dropdown.selectByIndex(random.nextInt(size)); // select an OPTION based on the generated random number
Related
i need to test a sorting function of a table. My problem is, that the developer implemented something like "lazy-loading" for the rows - the rows (div-elements of an ag-grid-table) only appears in the DOM, when they are in the view. So if you scroll through the table, the top elements disappear (even cant find the divs through dev-tools in chrome). Thats why i cant read all elements under the table and work with them - i need to scroll with my WebDriver through the table and read every row and write them into an array, where i can test, if the result is sorted.
WebElement table = Helper.driver.findElement(By.xpath("hidden"));
Actions builder = new Actions(Helper.driver);
//here i read the total number of elements in the table from a string, which displayed the total number directly under the table
String totalNumberOfRows = Helper.driver.findElement(By.xpath("hidden")).getText();
int totalInt = Integer.parseInt(Arrays.asList(total.split(" ")).get(1));
String[] sortedTableColumn = new String[totalInt];
List<WebElement> tableRows = table.findElements(By.className("ag-row")); //all active Elements, which exist in the DOM at the point, where the user looks. Mostly around 15 elements (35 total in my example)
for(int i = 0; i<totalInt; i++) {
builder.moveToElement(tableRows.get(i)).perform(); //scroll through the elements
sortedTableColumn[i] = Arrays.asList(tableRows.get(i).getText().split(" ")).get(0); //dont ask, its necessary to get the first part of the string
}
System.out.println(Arrays.toString(sortedTableColumn));
assertTrue(Ordering.natural().isOrdered(Arrays.asList(sortedTableColumn)));
problem is, that the loop end with an error, because the tableRow has only 15 elements and the loop want to go further. Pretty understandable.
But I dont know how to handle this. I need something like "take the tableRows, go one step down (tableRows will change), put this new tableRows and the old ones in a new array and kill duplicates. AND be sure, that this is the new List, where i use the iterator "i".
Has anyone an idea to fix/complete my code or maybe a better way to test sorting funtions of dynamic loaded tables?
I have a dynamic webtable where it has ID column. I want to get all links from this ID column and click on it one by one. Should I use list to save all the values and using for loop iterate through it? I am new to webdriver so not sure about it.
Yes you can simply use list and store all the links.
I have a list of many hyperlinks by below text :View / Select Monitors. So I stored all the links simply in a list.
And then I can click on any link by passing the value I want.
If you want to click on all the links then you can simply use a for loop and click it.
For example:
List<WebElement> list = driver.findElements(By.linkText("View / Select Monitors"));
//Click on first hyperlink with matched text
list.get(0).click();
2nd Example:
Step1.[Getting all the links by tagname]
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
Step2. [Storing the total size for the number of links]
int s =allLinks.size();
Step 3.[I am printing all the links here and there position]
for(int i=0;iString txtlink=allLinks.get(i).getText();
System.out.println("%n"+i);//Getting the value of i and hyperlink
System.out.println(txtlink);
allLinks.get(i).click;//You can simply pass the value of i and
click on it
`Thread.sleep(2000)'
}
In this way you can get the value of "i" also and then click if you like if you need to click on any particular link and if you want to click on all then simply use .click inside the loop.`enter code here`
I hope it helps.
How to click a row in table by randomly?
I know how to using Random class to iterate the List but don't know how to click the button of the specific row after randomize due to xpath different.
Please provide me some idea and guideline on how to solve this.
ps: I got some idea, the button is located at the last column, so i just click on the last column button.
Here is simple way to get random number from 1 to 10
int min=1;
int max=9;
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
System.out.println(randomNum);
If we think its row number, then, need to click on last column of that row.
i am imaging last column as td[6] then path will looks like
"//table/tbody/tr["+randomNum+"]/td[6]/button"
How to perform some actions on each value from a drop down using selenium java?
I am using below code. Please help.
WebElement bldgs=Fn_GetWebElement(CreateSSIObject.getProperty("Bldgselect"));
Select Bldg_select=new Select(bldgs);
List<WebElement> dropdownvalues = Bldg_select.getOptions();
int count=dropdownvalues.size();
System.out.println("Toatl number of values are :"+count);
for(int i=1;i<26;i++)
{
if(dropdownvalues.get(i).isEnabled())
{
dropdownvalues.get(i).click();
System.out.println("Not Working :"+i);
}
}
In order to select an option from select drop down, we need to call the methods on the select instance instead of the webelements.
For Eg,
Select Bldg_select=new Select(bldgs);
Bldg_select.selectByIndex(4); // selects 4th element in the drop down
you can't use click on the available options in the drop down, and this needs to be handled by using the select instance that we had created.
Refer this link to get an idea of various available options for selection a value in the drop down.Selenium Select Drop Down Options.
Thanks.
If you want to just print the values present in drop-down use following code, there's no need to click each item & select as .getOptions() already does that for you (i.e. fetches all the values inside Select Tag) so you can just traverse through the list and getText() of each element in the list.
for(int i=1;i<26;i++)
{
System.out.println("Not Working :"+ dropdownvalues.get(i).getText());
}
You're missing a click to expand the dropdown before trying to select the option. Meaning it will be trying to click on an element that's not visible. Add in:
bldgs.click();
Before your if to expand the dropdown
I have tried the code below but it's not working.
driver.get("http://www.naukri.com/");
WebElement element = driver.findElement(By.xpath("//input[#placeholder='Salary']"));
Select s1 = new Select(element);
s1.selectByVisibleText("5");
element is not a SELECT tag it just looks like one. You will need to click the INPUT element that you have in element and then click the LI that represents the desired amount, in the case listed: 5.
BTW, part of the site Terms & Conditions are that you won't scrape the site.
Naukri.com uses technological means to exclude Robots etc from crawling the website and scraping content. The user undertakes not to circumvent these methods.