简体   繁体   English

如何将匹配条件存储在可以使用硒单击的变量中

[英]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.

相关问题 如何使用带有硒的java检查条件,是否单击复选框 - How to check the condition, whether the checkbox is clicked or not using java with selenium 如何将值存储在从 selenium 的下拉列表中选择的变量中? - How to store the value in a variable which is selected from a dropdown in selenium? 如何从Selenium API获取Selenium变量,该变量指示Selenium仍在运行 - How can I get a selenium variable from selenium API which indicates selenium is still running 在哪里查看哪个JXBrowser和Selenium匹配的版本? - Where to check which JXBrowser & Selenium matched version? 有什么方法可以使用java将数组数据存储在变量中并进一步使用该变量 - Is there any way through which i can store array data in variable using java and use that variable further Selenium - 存储隐藏变量 - Selenium - Store hidden variable 问:如何设置用于if条件的变量 - Q: How to set variable which use for if condition 如何从XML文档中获取值并使用Selenium Java将其存储在变量中 - How to Fetch Value from XML document and store it in a variable using Selenium Java 如何使用Selenium Java从XML文档读取值并将其存储在变量中 - How to Read Value from XML document and store it in a variable using Selenium Java 如何在列表视图中获取单击项目的位置并将其存储在变量中 - How to get position of the clicked item in list view and store it in a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM