for loop value can not initialize with 0 in selenium webdriver/ java
String arr[] = new String[4];
for(int i = 0; i <4; i++) {
String text = driver
.findElement(By.xpath("html/body/select/option["+i+"]")).getText();
arr[i] = text;
System.out.println(arr[i]+" ");
}
This is html which i have to read all the element.
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
You can initialize the array with 0 but not in the scenario which you have specified.
You are finding an element with the xpath: html/body/select/option["+i+"]
where i is the position of the option element with respect to the select Element. It can never be zero.
If you want to start an array by zero then you should use the code specified below:
String arr[] = new String[4];
for(int i = 0; i <4; i++) {
String text = driver.findElements(By.xpath("html/body/select/option")).get(i).getText();
arr[i] = text;
System.out.println(arr[i]+" ");
}
Explanation: driver.findElements(By.xpath("html/body/select/option")) return the List of WebElement. You can get the element at index 0 which is supposed to be equivalent to "html/body/select/option[1]"
Html indexes starts from 1, not 0. The xpath should be
"html/body/select/option["+ (i + 1) +"]"
However, instead of locating the option one by one you can use Select class
WebElement dropdown = driver.findElement(By.xpath("html/body/select"));
Select select = new Select(dropdown);
List<WebElement> options = select.getOptions();
String arr[] = new String[options.size()];
for(int i = 0 ; i < options.size() ; i++) {
arr[i] = options.get(i).getText();
System.out.println(arr[i] + " ");
}
Related
HTML:
input type="checkbox" value="21-22-122-122" id="woWizardDevices_checkboxset_option_21-22-122-122|cb" data-oj-option-id="woWizardDevices_checkboxset_option_21-22-122-122" name="woWizardDevices_checkboxset_21-22-122-122" aria-label="[['Checkbox Select Row ' + row.item.data.id]]" class="oj-checkbox oj-component oj-enabled oj-component-initnode oj-selected" data-oj-internal="" placeholder="" data-oj-tabmod="NaN" tabindex="-1" xpath="1"
The ids keep on updating
I have tried:
List<WebElement> AllCheckboxes = driver.findElements(
By.xpath("//input[#type='checkbox' and starts-with(#id,'woWizardDevices_checkboxset_option_21-22')]"));
int size = AllCheckboxes.size();
for (int i = 0; i < size; i++) {
AllCheckboxes.get(i).click();
break;
}
but it fails.
List<WebElement> tdata=driver.findElements(By.tagName("tr"));
List<WebElement> tdata1=driver1.findElements(By.tagName("tr"));
for(int i=0,j=0; i<tdata.size() && j<tdata1.size();i++,j++ )
{
WebElement row = tdata.get(i);
WebElement row1 = tdata.get(j);
System.out.print(row1.getText());
System.out.print(row1);
if(row.getText().equals(row1.getText()))
{
System.out.println(row.getText());
}
else if(!(row.getText().equals(row1.getText())))
{
System.out.print("Not matching text");
System.out.println(row1.getText());
}
}
This is my code for comparing 2 web table, I am Unable to verify content equal or not equal. For unmatched text also it is not printing anything. else if part is not capturing if elements are not equal.
try
List<WebElement> tdata= ...
List<WebElement> tdata1= ...
int common= 0;
for (WebElement element: tdata)
if (tdata1.contains(element))
common++;
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());
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();
}
I have a piece of HTML like this:
<a href="/something">
Title
<span>Author</span>
</a>
I got a WebElement that matches this HTML. How can I extract only "Title" from it? Method .getText() returns "Title\nAuthor"...
You can't do this in the WebDriver API, you have to do it in your code. For example:
var textOfA = theAElement.getText();
var textOfSpan = theSpanElement.getText();
var text = textOfA.substr(0, textOfA.length - textOfSpan.length).trim('\n');
Note that the trailing newline is actually part of the text of the <a> element, so if you don't want it, you need to strip it.
Here is the method developed in python.
def get_text_exclude_children(element):
return driver.execute_script(
"""
var parent = arguments[0];
var child = parent.firstChild;
var textValue = "";
while(child) {
if (child.nodeType === Node.TEXT_NODE)
textValue += child.textContent;
child = child.nextSibling;
}
return textValue;""",
element).strip()
How to use in this:
liElement = driver.find_element_by_xpath("//a[#href='your_href_goes_here']")
liOnlyText = get_text_exclude_children(liElement)
print(liOnlyText)
Please use your possible strategy to get the element, this method need an element from which you need the text (without children text).
If using Python:
[x['textContent'].strip() for x in element.get_property('childNodes') if isinstance(x, dict)]
Where element is your element.
This will return ['Title', ''] (because there are spaces after span).
you can use jsexecutor to iterate the child nodes, trap the textNode 'Title' and then return its content like below
WebElement link = driver.findElement(By.xpath("//a[#href='something']"));
JavascriptExecutor js = ((JavascriptExecutor)driver);
String authorText = (String) js.executeScript("for(var i = 0; i < arguments[0].childNodes.length; i++) {
if(arguments[0].childNodes[i].nodeName == \"#text\") { return arguments[0].childNodes[i].textContent; } }", link);
The javascript code block above iterates both textNode ('Title') and SPAN ('Author') but returns only the text content of textNode.
Note: Previous to this, I have tried including text node in xpath like below, but webdriver throws invalidselector exception as it requires element not textnode
WebElement link = driver.findElement(By.xpath("//a[#href='something']/text()"));
Verify the element present for "//a[normalize-space(text())=Title]". It will return true if the text present inside 'a' tag is 'Title'.