HTML code image
I need getText from the li to validate correct option is visible.
I have try 3 different option but none of they work correctly.
I need 'boundlist-selected' class get text this will validate selected text.
Have comment out output inside the code.
public void DDList(WebDriver driver, String actualViews,String ReportName) throws InterruptedException {
waitForElementPresent(driver, 30, expand_btn1);
Thread.sleep(1000);
By ddlselect = By.xpath("//div[input[#name='"+ReportName+"']]/following-sibling::div");
waitForElementPresent(driver, 30, ddlselect,ReportName);
click(driver,ddlselect,ReportName);
Thread.sleep(2000);
waitForLoad(driver, 60);
By ddlexpand = By.xpath("//ul/li[text()='"+actualViews+"']");
waitForElementPresent(driver, 30, ddlexpand ,actualViews);
click(driver,ddlexpand,actualViews);
Thread.sleep(2000);
//try 1
WebElement textlabel = driver.findElement(By.xpath("//li[contains(#class,'boundlist-selected')]"));
value = textlabel.getAttribute("value");
System.out.println("getAttribute value" + value); // value Ouput 0
//try 2
String textlabel2 = driver.findElement(By.xpath("//li[contains(#class,'boundlist-selected')]")).getText();
System.out.println(textlabel2 + "print text"); // blank output
// try 3
List<WebElement>allProduct = driver.findElements(By.xpath("//ul[#class='x-list-plain']/li"));
for( WebElement product : allProduct){
System.out.println(product.getText());
// This print correctly all 3 value now I need to validate select text how can I do that?
}
// below code to validate select gets fail as value is blank
if (value.equals(actualViews)) {
Reporter.log(
"Successfully able to select " + actualViews + "option from dashboard" +ReportName+ "summary screen");
Add_Log.info(
"Successfully able to select " + actualViews + "option from dashboard" +ReportName+ "summary screen");
} else {
Assert.fail();
Reporter.log("Not able to select " + actualViews + "option from dashboard" +ReportName+ "summary screen");
Add_Log.info("Not able to select " + actualViews + "option from dashboard" +ReportName+ "summary screen");
}
Try code below:
//try 1
String selectedText = driver.findElement(By.cssSelector("li.x-boundlist-selected")).getText();
System.out.println(selectedText)
//try 2
selectedText = driver.findElement(By.cssSelector("li.x-boundlist-selected")).getAttribute("textContent");
System.out.println(selectedText)
Related
public void itemStateChanged(ItemEvent e)
{
text += "Language : ";
text += "Hindi: " + Hindi.getState();
text += " English: " + English.getState();
text += "Maths: " + Maths.getState();
label.setText("");
label.setText(text);
}
this code show new result with previous one i want updated result only not the previous one so how can i remove previous AWT label output from the frame.
just remove the + in the first line of the method.
text = "Language : ";
Please note that the way you are appending Strings is not efficient. Try using String.format() instead.
I am trying to extract the value of an HTML table element from a website and compare it to a user input value but it seems that the nested loop is not being entered when I run the program. It works with no errors but I am not getting any output from Eclipse, I'm new to Selenium Java and still learning.
See my code below:
String inputString = basePrem;
try {
//Print to console the value of Base Prem
WebElement table = driver.findElement(By.xpath(".//td[text()='Base Premium']/following-sibling::*"));
List<WebElement> allrows = table.findElements(By.tagName("tr"));
List<WebElement> allcols = table.findElements(By.tagName("td"));
for (WebElement row: allrows) {
List<WebElement> Cells = row.findElements(By.tagName("td"));
for (WebElement Cell:Cells) {
if (Cell.getText().contains(basePrem)) {
System.out.print("Base Premium = "+ basePrem + " ");
}
else if (!Cell.getText().contains(basePrem))
{
System.out.print("Base Premium = " + basePrem + " ");
break;
}
}
}
}
catch (Exception e) {
errorMessage = "Value discrepancy";
System.out.println(errorMessage + " - " + e.getMessage());
driver.close();
}
Also, inputString is where I input the value I use for comparison (I use a separate excel file for testing)
Since the control is not going inside the nested loop, I probably have some logical error?
You can rewrite the code as below and then validate whether your inputstring is available in the table. It is not necessary to use nested for loops
Code:
String inputString = basePrem;
WebElement table = driver.findElement(By.xpath(".//table"));
//Extract all the Cell Data Element
List<WebElement> dataElementList=table.findElements(By.xpath(".//td"));
for(WebElement dataElement : dataElementList){
if(dataElement.getText().contains(inputString)){
System.out.print("Base Premium = "+ basePrem + " ");
break;
}
}
How do i create the one line expression using Java swing, link image picture. the every minute, every day,every month, every weekday and every hour need to convert it to "*" and also all the combo box contain the list of number list number link and weekday contain the click the picture
what i want is, if the user select "Every Minute" , "Every day","month = 2", "Weekday = monday", "hour= 3"
note of weekday JCombo : sunday = 0 , monday = 1, tuesday = 2 .....
the output will print as : * * 2 1 3
thanks alot.
i already tried this , my beginning code but cant do much :
String sjcb_EM = jcb_EM.getSelectedItem().toString();
String sjcb_EH = jcb_EH.getSelectedItem().toString();
String sjcb_ED = jcb_ED.getSelectedItem().toString();
String sjcb_EEM = jcb_EEM.getSelectedItem().toString();
String sjcb_EW = jcb_EW.getSelectedItem().toString();
String vb_1 = sjcb_EM + " " + sjcb_EH + " " + sjcb_ED + " " + sjcb_EEM + " " + sjcb_EW;
System.out.println(vb_1);
now i stuck, how to make the expression that i wanted.
Start by building a class which can hold both the display value and the query value...
public class WorkoutUnit {
private String displayValue;
private String queryValue;
public WorkoutUnit(String displayValue, String queryValue) {
this.displayValue = displayValue;
this.queryValue = queryValue;
}
public String getDisplayValue() {
return displayValue;
}
public String getQueryValue() {
return queryValue;
}
#Override
public String toString() {
return displayValue;
}
}
Build a ComboBoxModel using these values...
DefaultComboBoxModel<WorkoutUnit> model = new DefaultComboBoxModel<>();
model.addElement(new WorkoutUnit("Every Minute", "*"));
for (int index = 10; index < 61; index += 10) {
model.addElement(new WorkoutUnit(Integer.toString(index), Integer.toString(index)));
}
JComboBox<WorkoutUnit> cb = new JComboBox(model);
When needed, get the selected item from the combo box and get its query value...
WorkoutUnit unit = (WorkoutUnit)cb.getSelectedItem();
System.out.println("Query = " + unit.getQueryValue());
In this example, I've used toString to provide the display value to the JComboBox, this is not my preferred solution, I'd prefer to use a ListCellRenderer as demonstrated here
Oh, and because it looks like you're heading down a database query route, you should also have a look at Using Prepared Statements
If I'm expecting a page to display 5 offers, how would I tell webdriver to list all those 5 offers in a ul?
The Ul html code is
<ul id="more-load" class="product_list_widget pagination-centered" style="padding-top:15px;">
I think you would use
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='more-load']/ul/li"));
for (WebElement element: allElements) {
System.out.println(element.getText());
}
but I'm not sure how to print each individual offer in the Ul and match the 5 offers expected to be displayed
EDITED CODE
never mind used this and worked
WebElement allElements = driver.findElement(By.id("more-load"));
List<WebElement> liElements = allElements.findElements(By.tagName("li"));
for (int i = 0; i < liElements.size(); i++)
{
System.out.println("-------------------------------------------------------");
System.out.println(liElements.get(i).getText());
}
however if I have a column on the left side, with ul = product categories, how would I loop it to go through each individual link text and preform the same function
Let's say your 5 offers are as below:
Offer1
Offer2
Offer3
Offer4
Offer5
now your approach is correct till for loop:
then you can do something like :
String offerString = Offer1 + " " + Offer2 + " " + Offer3 + " " + Offer4 + " " + Offer5;
for (WebElement element: allElements) {
if(offerString.contains(element.getText()){
System.out.println("Offer item is: " + element.getText());
}
}
Stick with your original attempt, except change the line:
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='more-load']/ul/li"));
to
List<WebElement> allElements = driver.findElements(By.xpath("//ul[#id='more-load']/li"));
using java8:
List<String> offers = driver
.findElements(By.cssSelector("#more-load li"))
.stream()
.map(WebElement::getText)
.collect(Collectors.toList());
and you can assert offers.size() to have some expected value by any assert library
To get linked text elements in UL you can use next selector:
driver.findElements(By.cssSelector("ul a"))
I'm using Selenium to loop through an ArrayList of Strings in order to use each string in an xPath expression in order to select its appropriate checkbox on a website.
The problem is, when I use the for loop, the variable containing the string doesn't seem to create a valid xPath, yet when I simply substitute the string in myself it works fine.
For example, here is my ArrayList declaration with some values added.
ArrayList<String> fieldList = new ArrayList<String>();
fieldList.add("Street");
fieldList.add("City");
fieldList.add("Country");
If I then use the following code, it goes into the catch block
WebDriverWait waitForElement = new WebDriverWait(driver, 1);
for (String cField: fieldList) {
try {
waitForElement.until(ExpectedConditions.elementToBeClickable(By.xpath("//td[following-sibling::td[2] = " + cField + "]/input")));
WebElement checkBox = driver.findElement(By.xpath("//td[following-sibling::td[2] = " + cField + "]/input"));
checkBox.click();
} catch (Exception error) {
System.out.println("Couldn't find " + cField);
}
}
Telling me it couldn't find "Street" for example.
Yet when my try block contains the following, with the value explicitly stated, it works:
waitForElement.until(ExpectedConditions.elementToBeClickable(By.xpath("//td[following-sibling::td[2] = 'Street']/input")));
WebElement checkBox = driver.findElement(By.xpath("//td[following-sibling::td[2] = 'Street']/input"));
What am I doing wrong? Thanks a lot.
You are forgetting to quote your strings in the XPath expression. Add single quotes around cField:
waitForElement.until(ExpectedConditions.elementToBeClickable(
By.xpath("//td[following-sibling::td[2] = '" + cField + "']/input")));
// quotes added here ---^ and here ---^
WebElement checkBox =
driver.findElement(By.xpath("//td[following-sibling::td[2] = '" + cField + "']/input"));
// quotes added here ---^ and here ---^