无法更改webdriver中的单选按钮值

时间:2016-03-22 05:46:33

标签: html selenium

在这个html代码中,现金被选为默认值,我想将无线电值更改为卡片。

<label>
    <input id="cash" type="radio" checked="" value="CS" name="cash_cash">
    <i></i>CASH
</label>
<label>
    <input id="card" type="radio" value="CD" name="cash_cash">
    <i></i>Card
</label>

我使用了以下代码,但没有一个给我结果。

方法1

WebElement pl=driver.findElement(By.xpath("//input[@name='cash_cash'and @value='card']"));
pl.click();

方法2

List<WebElement> radiobuttons = driver.findElements(By.id("card"));
for(WebElement radio:radiobuttons)
{
    String cvalue = radio.getAttribute("value");
    if(cvalue .equals("CD"))
    {
        radio.click();
    }
    else
    {
        System.out.println("fails");
    }
}

int rcount = radiobuttons.size();

上面的行给我rcount为4,尽管只有两个选项。

请帮助我(这是一个弹出窗口)。 HTML source

2 个答案:

答案 0 :(得分:0)

方法1中的XPath表达式不正确。它应该是@id='card'而不是@value='card'。请参阅以下代码:

解决方案1 ​​:By.xpath

List<WebElement> radiobuttons = driver.findElements(By.xpath("//input[@name='cash_cash'and @id='card']"));
for(WebElement radio:radiobuttons)
{
    if(radio.isDisplayed()){
        radio.click();
        break;
    }   
}

解决方案2 :By.cssSelector

List<WebElement> radiobuttons = driver.findElements(By.cssSelector("input#card[value='CD']"));
for(WebElement radio:radiobuttons)
{
    if(radio.isDisplayed()){
        radio.click();
        break;
    }
}   

答案 1 :(得分:0)

package com.androidexample.screenonoff;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class AEScreenOnOffService extends Service {
    BroadcastReceiver mReceiver=null;

        @Override
        public void onCreate() {
            super.onCreate();

            // Toast.makeText(getBaseContext(), "Service on create", Toast.LENGTH_SHORT).show();

            // Register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            mReceiver = new AEScreenOnOffReceiver();
            registerReceiver(mReceiver, filter);

        }


        @Override
        public void onStart(Intent intent, int startId) { 

            boolean screenOn = true;

            try{
                // Get ON/OFF values sent from receiver ( AEScreenOnOffReceiver.java ) 
                screenOn = intent.getBooleanExtra("screen_state", false);

            }catch(Exception e){}

             //  Toast.makeText(getBaseContext(), "Service on start :"+screenOn, 
                    //Toast.LENGTH_SHORT).show();

            if (!screenOn) {

                // your code here
                // Some time required to start any service
                //Toast.makeText(getBaseContext(), "Screen on, ", Toast.LENGTH_LONG).show();


            } else {

                Intent yourintent = new Intent(this, shoad.class);
                yourintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(yourintent);
                // your code here
                // Some time required to stop any service to save battery consumption
                //Toast.makeText(getBaseContext(), "Screen off,", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void onDestroy() {

            Log.i("ScreenOnOff", "Service  distroy");
            if(mReceiver!=null)
             unregisterReceiver(mReceiver);

        }
}

的Thread.sleep(5000);  driver.findElement(By.xpath( “.//*[@ ID = 'payment_add_form'] / DIV / DIV [1] / H3 /跨度/ DIV [2] /标签/ I”))点击(); < / p>