if语句(element.click或break)不起作用

时间:2016-05-12 22:51:48

标签: java selenium-webdriver

我有一个<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="100dp" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorAccent" app:layout_collapseMode="parallax" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/cardList" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> 标签列表,我需要点击包含特定号码的标签(让我们说4)。

<li>

我的代码是:

<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>

Eclipse结果是:

int a = 4;
List<WebElement> allElements = driver.findElements(By.xpath("//div[@class='divClass']/ul[@class='ulClass']/li")); 
    for (WebElement element: allElements) {
        String bText = element.getText();
        int b = Integer.parseInt(bText);
        System.out.println(a + " ? " + b);
        if (a == b){
            element.click();
            break;
        }
    }

我无法理解1 ? 4 2 ? 4 3 ? 4 4 ? 4 5 ? 4 声明的错误(ifelement.click();无效)...有人可以帮忙吗?

4 个答案:

答案 0 :(得分:1)

我认为您可以通过

更改xpath表达式
"//div[@class='divClass']/ul[@class='ulClass']/li[text()='4']"

使用您想要的值获取li标签

答案 1 :(得分:1)

尝试以下代码: -

String value = driver.findElement(By.xpath("//div[@class='divClass']/ul[@class='ulClass']/li[4]")).getText();

希望它会对你有所帮助:)。

答案 2 :(得分:1)

    Actually the control is not going inside if loop, 

    use below approch

    String a = "4";
    List<WebElement> allElements = driver.findElements(By.xpath("//div[@class='divClass']/ul[@class='ulClass']/li")); 
        for (WebElement element: allElements) {
            String bText = element.getText();
            System.out.println(a + " ? " + bText );
            if (a.equals(bText)){
                element.click();
                break;
            }
        }



OR

Use javascript click instead of normal click....................... 

    String a = "4";
    List<WebElement> allElements = driver.findElements(By.xpath("//div[@class='divClass']/ul[@class='ulClass']/li"));`enter code here`
for(int i=0; i<allElements.size;i++)
{
if(a.equals(allElements.get(i).gettext(bText))
{
javascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", allElements.get(i));
}   
}

答案 3 :(得分:0)

@ Manjunatha.N 我不确切地知道为什么控件没有进入if语句,但我设法让它工作:

int a = 4;
List<WebElement> allElements = driver.findElements(By.xpath("//div[@class='divClass']/ul[@class='ulClass']/li")); 
    for (WebElement element: allElements) {
        String bText = element.getText();
        int b = Integer.parseInt(bText);
        if (a != b) {
            System.out.println(a + " = " + b + "? are not equal!");
        } else {
            System.out.println(a + " = " + b + "? are equal!");
            element.click();
            break;
        }
    }

Eclipse输出现在是:

1 = 4? are not equal!
2 = 4? are not equal!
3 = 4? are not equal!
4 = 4? are equal!

然后点击所需元素!!!!