[英]How to store the matched condition in a variable which can be clicked using selenium
I have a value in excel and checking the existence in array of elements in drop down. 我在excel中有一个值,并在下拉列表中检查元素数组中是否存在。 Where the condition passes, I want to store that and click.
条件通过的地方,我要存储它并单击。 I tried to store it in a string but, it is not clickable.
我试图将其存储在字符串中,但是它不可单击。 I tried to store in a web element but failed.
我试图将其存储在Web元素中,但失败了。 kindly help.
请帮助。
List<WebElement> countrylist = driver.findElements(By.cssSelector("ul.ui-multiselect-checkboxes li label[for*='ddlBU'] span"));
List<String> all_countrylist = new ArrayList<>();
Thread.sleep(1000);
String selectedcountry = driver.findElement(By.xpath("html/body/div[9]/ul/li[2]/label/span")).getText();
WebElement clickselectedcountry = driver.findElement(By.xpath("html/body/div[9]/ul/li[2]/label/span"));
Thread.sleep(1000);
for(int cui = 0; cui < countrylist.size(); cui++)
{
all_countrylist.add(countrylist.get(cui).getText());
if((countrylist.get(cui).getText()).equalsIgnoreCase(countrysheet.getCell(0, 2).getContents()))
{
System.out.println("the Country " + (countrysheet.getCell(0, 2).getContents()) + " which is existing");
String clickcountry = (countrylist.get(cui).getText());
}
else
{
System.out.println("\nSelected Country " + (countrysheet.getCell(0, 2).getContents()) + " which is not existing");
}
}
Try below code. 尝试下面的代码。
String countryToBeSelected = countrysheet.getCell(0, 2).getContents();
By selectedCountryLocator = By.xpath("html/body/div[9]/ul/li[2]/label/span");
String selectedcountry=driver.findElement(selectedCountryLocator).getText();
//If Country is other than selected country, then click and open the list
if(!countryToBeSelected.equals(selectedcountry)){
WebElement clickselectedcountry=driver.findElement(selectedCountryLocator);
clickselectedcountry.click();
//Get List Of All Countries In WebElement List
List<WebElement> countrylist = driver.findElements(By.cssSelector("ul.ui-multiselect-checkboxes li label[for*='ddlBU'] span"));
//Store all country details in list
List<String> all_countrylist=new ArrayList<String>();
//Iterate over WebElement list and store country values in string list
for(WebElement country : countrylist){
all_countrylist.add(country.getText());
}
//Get the expected country index
int elementIndex = all_countrylist.indexOf(countryToBeSelected);
//Click on After finding the Country Index
countrylist.get(elementIndex).click();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.