While working on selenium,I have the xpath
//tbody[#id='se-tbody-result']//tr[3]//a
I want to loop tr[value] from tr[3] to tr[20].
Such xPath would return you values from tr[3] til tr[20]
//tbody[#id='se-tbody-result']//tr[position()>=3 and position()<=20]//a
Simple for loop in java works for me
public void sample() {
for (int i=3; i<=20; i++) {
driver.findElement(By.xpath("//tbody[#id='se-tbody-result']//tr["+ i + "]//a"));
}
Use like below if in python-
trs = driver.find_elements_by_xpath("//tbody[#id='se-tbody-result']//tr")
for i in trs[3:20]:
my_a = i.find_element_by_xpath(".//a")
print my_a.get_attribute('href')
Related
I'm new to Geb and fairly new in Java. I ask my self if its possible to call multiple methods through a loop. For example this part:
homePage.file1 = Content.Upload()
isDisplayed(homePage.clear1, true)
homePage.file2 = Content.Upload()
isDisplayed(homePage.clear2, true)
homePage.file3 = Content.Upload()
isDisplayed(homePage.clear3, true)
I had the idea to call this through a loop cause the names are very similar to each other. Only the numbers are different.
So I thought about something like this:
String[] elements = { "file1", "file2","file3"}
for( int i = 0; i <= elements.length - 1; i++){
homePage.elements[i] = Generator.fileUpload()
}
But this won't work. Is there any other way to get this to work?
Greetings
Think this is what you're trying to achieve?:
def elements = ["file1", "file2","file3"]
elements.each {
homePage."${it}" = Generator.fileUpload()
}
I'm trying to show all the selected options from a multi select drop down list. But not getting the proper way to do this. Please help me on this.
Here is the html code for the drop down:
<select multiple id="fruits">
<option value="banana">Banana</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="grape">Grape</option>
</select>
Here is the code i'm trying with:
public void dropDownOperations()
{
driver.get("http://output.jsbin.com/osebed/2");
Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
String currentvalue = DDLIST.getFirstSelectedOption().getText();
System.out.println(currentvalue);
DDLIST.selectByIndex(1);
String currentvalue1 = DDLIST.getFirstSelectedOption().getText();
System.out.println(currentvalue1);
}
I also tried with this code:
Here i'm getting this output:
[[[[[ChromeDriver: chrome on XP (69aee19e9922ca218ff47c0ccdf1bbbc)] ->
id: fruits]] -> tag name: option], [[[[ChromeDriver: chrome on XP
(69aee19e9922ca218ff47c0ccdf1bbbc)] -> id: fruits]] -> tag name:
option]]
public void dropDownOperations1()
{
driver.get("http://output.jsbin.com/osebed/2");
Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
DDLIST.selectByIndex(1);
List<WebElement> currentvalue1 = DDLIST.getAllSelectedOptions();
System.out.println(currentvalue1);
}
Your second approach should work fine with a minor fix. getAllSelectedOptions() will return a List of selected options as WebElement. You need to iterate over the list to get the text from WebElement.
List<WebElement> selectedOptions = DDLIST.getAllSelectedOptions();
for (WebElement option : selectedOptions){
System.out.println(option.getText());
}
Try This:
List<WebElement> allSelected = select.getAllSelectedOptions();
Iterator itr = allSelected.iterator();
while(itr.hasNext()){
WebElement item = (WebElement) itr.next();
System.out.println(item.getText());
}
Try this below code, It will select one by one options from dropdown.
Select DDLIST = new Select(driver.findElement(By.id("fruits")));
DDLIST.selectByIndex(0);
DDLIST.selectByIndex(1);
List<WebElement> selectedOptions = DDLIST.getAllSelectedOptions();
for(int i=0; i<selectedOptions.size(); i++)
{
System.out.println(DDLIST.getOptions().get(i).getText());
}
Try This:
System.out.println(DDLIST.selectByIndex(0).getText());
System.out.println(DDLIST.selectByIndex(1).getText());
And so on. Instead of using a variable and trying it.
Try this:
Select DDLIST = new Select (driver.findElement(By.id("fruits")));
for(int i=0; i<DDLIST.getOptions().size(); i++)
System.out.println(DDLIST.getOptions().get(i).getText());
I am trying below code but its throwing similar result every time.. Let me know if i am doing something wrong
driver.get("https://www.google.com/search?hl=en&gl=in&tbm=nws&authuser=0&q=The+Telegraph%27s+Production+Manager+To+Take+Over+The&gws_rd=ssl");
java.util.List<WebElement> competitor_name = driver.findElements(By.className("slp"));
for (int i = 0; i < competitor_name.size(); i++)
{
String cmp_name = competitor_name.get(i).findElement(By.xpath("//span[#class='_tQb _IId']")).getText();
System.out.println("Competitor name is : "+cmp_name );
}
Can you try below way which will build correct xpath so that we get out of this issue
String cmp_name = competitor_name.get(i).findElement(By.xpath("span[#class='_tQb _IId']")).getText();
How do I increment value of img path when said path looks like this?
//ab[x]/img
X value increasing by 1 and has a limit of 50.
Trying to write a test case on how to click on several images on website.
Edit: Just wanted to add that I'm just starting with Selenium IDE and using standart commands.
Solution 1: Format your xpath path selector
for(int i=1; i<=numberOfImages; i++) {
String path = String.format("//ab[%d]/img", i);
WebElement image = driver.findElement(By.xpath(path));
if(image != null) {
image.click();
}
}
Solution 2: Select all elements that "//ab/img" returns and iterate over them.
String path = "//ab/img";
List<WebElement> imgElements = driver.findElements(By.xpath(path)); //notice the plural
for(WebElement image : imgElements) {
image.click();
}
In selenium 2.0, i am trying to get the list of drop down values and to print it. How to do this? I am trying below:
for (int i = 1;i<=13;i++)
{
WebElement values=driver.findElement(By.xpath("//li[#rel='i']/a/span[#class='pull-left']"));
System.out.println(values);
}
#rel= '1', '2' should go like this.. so that i can print all the values.
But this is not working.. How to use the 'i' in this element.
Thanks
Instead of hard coding no of options value you can get that dynamically.
List<WebElement> options = driver.findElements(By.xpath("//ul/li/a/span[#class='pull-left']"));
//iterate above list to get all option values
for(WebElement eachOption : options) {
System.out.println(eachOption.getText());
}
Some how i managed to get this..
for (int i = 1;i<=13;i++) {
//System.out.println("//li[#rel=" + i +"]/a/span[#class='pull-left']");
String values=driver.findElement(By.xpath("//li[#rel=" + i +"]/a/span[#class='pull-left']")).getText();
System.out.println(values); }