根据文本访问div内的无线电

时间:2017-05-03 11:49:16

标签: java selenium selenium-webdriver

我有多个div,每个单选按钮,我要找的是获得Tilte并点击每个div的单选按钮,。 这是HTML。

DEFINE JOB EXPORT_DELIMITED_FILE
DESCRIPTION 'Export rows from a Teradata table to a delimited file'
(
    DEFINE SCHEMA FILE_SCHEMA
    (
        Column1 VARCHAR(100),
        Column2 VARCHAR(100),
        Column3 VARCHAR(100),
        Column4 VARCHAR(100)
    );

    DEFINE OPERATOR SQL_SELECTOR
    TYPE SELECTOR
    SCHEMA FILE_SCHEMA
    ATTRIBUTES
    (
        VARCHAR PrivateLogName = 'selector_log',
        VARCHAR TdpId = 'Server',
        VARCHAR UserName = 'user',
        VARCHAR UserPassword = 'password',
        VARCHAR SelectStmt = 'SELECT CAST(ColumnA AS VARCHAR(100), CAST(ColumnB AS VARCHAR(100), CAST(ColumnC AS VARCHAR(100), CAST(COUNT(*) AS VARCHAR(100)) FROM MyTable GROUP BY 1,2,3;',
        VARCHAR ReportModeOn = 'Y'
    );

    DEFINE OPERATOR FILE_WRITER
    TYPE DATACONNECTOR CONSUMER
    SCHEMA *
    ATTRIBUTES
    (
        VARCHAR PrivateLogName = 'dataconnector_log',
        VARCHAR DirectoryPath = 'E:\PowerShell\Output',
        VARCHAR FileName = 'test_file.txt',
        VARCHAR Format = 'Delimited',
        VARCHAR OpenMode = 'Write',
        VARCHAR TextDelimiter = '|'
    );

    APPLY TO OPERATOR (FILE_WRITER)
    SELECT * FROM OPERATOR (SQL_SELECTOR);
);

我试过以下但不行:

<div id="iff-campaigns">
    <div id="iff-campaign52249" class="iff-campaign-container iffhidden" onmouseover="IffAdvertismentAction(52249,&quot;over&quot;);" onmouseout="IffAdvertismentAction(52249,&quot;out&quot;);">
        <div class="iff-title">Would you like to get paid to take Surveys and Fun IQ Tests?</div>
        <div class="iff-optin">
            <label>
                <input type="radio" name="iffinput-52249" id="iffinputyes-52249" value="Yes" onclick="IffAdvertismentSelect(52249,false,&quot;&quot;,false);">Yes</label>
            <label>
                <input type="radio" name="iffinput-52249" id="iffinputno-52249" value="No" onclick="IffAdvertismentSelect(52249,false,&quot;&quot;,false);">No</label>
        </div>
        <div id="div-cq52249" class="iff-div-customquestions-survey">
            <div class="clear"></div>
            <div id="div-first5224915243" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> First Name: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="ifffirst~52249~15243" name="ifffirst~52249~15243" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;ifffirst~52249~15243&quot;,&quot;first&quot;,1,1);" regfld=""> </div>
            </div>
            <div class="clear"></div>
            <div id="div-last5224915244" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> Last Name: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="ifflast~52249~15244" name="ifflast~52249~15244" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;ifflast~52249~15244&quot;,&quot;last&quot;,1,1);" regfld=""> </div>
            </div>
            <div class="clear"></div>
            <div id="div-email5224915245" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> Email Address: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="iffemail~52249~15245" name="iffemail~52249~15245" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffemail~52249~15245&quot;,&quot;email&quot;,1,1);" regfld="^[^<>s@]+(@[^<>s@]+(.[^<>s@]+)+)$"> </div>
            </div>
            <div class="clear"></div>
        </div>
        <div id="iff-rollup52249" class="iff-roleup"> </div>
    </div>
    <div id="iff-campaign52706" class="iff-campaign-container" onmouseover="IffAdvertismentAction(52706,&quot;over&quot;);" onmouseout="IffAdvertismentAction(52706,&quot;out&quot;);">
        <div class="iff-title">Would you like the days important news broken down into a quick and informative read each morning?</div>
        <div class="iff-optin">
            <label>
                <input type="radio" name="iffinput-52706" id="iffinputyes-52706" value="Yes" onclick="IffAdvertismentSelect(52706,false,&quot;&quot;,false);">Yes</label>
            <label>
                <input type="radio" name="iffinput-52706" id="iffinputno-52706" value="No" onclick="IffAdvertismentSelect(52706,false,&quot;&quot;,false);">No</label>
        </div>
        <div id="div-cq52706" class="iff-div-customquestions-survey">
            <div class="clear"></div>
            <div id="div-email5270620435" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> Email Address: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="iffemail~52706~20435" name="iffemail~52706~20435" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffemail~52706~20435&quot;,&quot;email&quot;,1,1);" regfld="^[^<>s@]+(@[^<>s@]+(.[^<>s@]+)+)$"> </div>
            </div>
            <div class="clear"></div>
        </div>
        <div id="iff-rollup52706" class="iff-roleup"> </div>
    </div>
    <div id="iff-campaign52912" class="iff-campaign-container" onmouseover="IffAdvertismentAction(52912,&quot;over&quot;);" onmouseout="IffAdvertismentAction(52912,&quot;out&quot;);">
        <div class="iff-title">Ready to take cooking to the next level? </div>
        <div class="iff-optin">
            <label>
                <input type="radio" name="iffinput-52912" id="iffinputyes-52912" value="Yes" onclick="IffAdvertismentSelect(52912,false,&quot;&quot;,false);">Yes</label>
            <label>
                <input type="radio" name="iffinput-52912" id="iffinputno-52912" value="No" onclick="IffAdvertismentSelect(52912,false,&quot;&quot;,false);">No</label>
        </div>
        <div id="div-cq52912" class="iff-div-customquestions-survey">
            <div class="clear"></div>
            <div id="div-first5291222515" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> First Name: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="ifffirst~52912~22515" name="ifffirst~52912~22515" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;ifffirst~52912~22515&quot;,&quot;first&quot;,1,1);" regfld=""> </div>
            </div>
            <div class="clear"></div>
            <div id="div-last5291222516" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> Last Name: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="ifflast~52912~22516" name="ifflast~52912~22516" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;ifflast~52912~22516&quot;,&quot;last&quot;,1,1);" regfld=""> </div>
            </div>
            <div class="clear"></div>
            <div id="div-email5291222517" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> Email Address: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="iffemail~52912~22517" name="iffemail~52912~22517" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffemail~52912~22517&quot;,&quot;email&quot;,1,1);" regfld="^[^<>s@]+(@[^<>s@]+(.[^<>s@]+)+)$"> </div>
            </div>
            <div class="clear"></div>
            <div id="div-zip5291222522" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> Zip: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="iffzip~52912~22522" name="iffzip~52912~22522" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffzip~52912~22522&quot;,&quot;zip&quot;,1,1);" regfld="(^d{5}$)|(^d{5}-d{4}$)|^([ABCEGHJKLMNPRSTVXY]d[ABCEGHJKLMNPRSTVWXYZ])+{0,1}(d[ABCEGHJKLMNPRSTVWXYZ]d)$"> </div>
            </div>
            <div class="clear"></div>
        </div>
        <div id="iff-rollup52912" class="iff-roleup"> </div>
    </div>
    <div id="iff-campaign52230" class="iff-campaign-container" onmouseover="IffAdvertismentAction(52230,&quot;over&quot;);" onmouseout="IffAdvertismentAction(52230,&quot;out&quot;);">
        <div class="iff-title">Would you like to receive FREE Severe Local Weather Alerts straight to your inbox? These can be life saving! </div>
        <div class="iff-optin">
            <label>
                <input type="radio" name="iffinput-52230" id="iffinputyes-52230" value="Yes" onclick="IffAdvertismentSelect(52230,false,&quot;&quot;,false);">Yes</label>
            <label>
                <input type="radio" name="iffinput-52230" id="iffinputno-52230" value="No" onclick="IffAdvertismentSelect(52230,false,&quot;&quot;,false);">No</label>
        </div>
        <div id="div-cq52230" class="iff-div-customquestions-survey">
            <div class="clear"></div>
            <div id="div-email5223015020" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> Email Address: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="iffemail~52230~15020" name="iffemail~52230~15020" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffemail~52230~15020&quot;,&quot;email&quot;,1,1);" regfld="^[^<>s@]+(@[^<>s@]+(.[^<>s@]+)+)$"> </div>
            </div>
            <div class="clear"></div>
            <div id="div-zip5223015025" class="iff-customquestion">
                <div class="iff-cqlabel"> <span>*&nbsp;</span> Zip: </div>
                <div class="iff-cqcontrol">
                    <input type="text" id="iffzip~52230~15025" name="iffzip~52230~15025" value="" class="iff-input-text" onblur="Ifffieldchange(&quot;iffzip~52230~15025&quot;,&quot;zip&quot;,1,1);" regfld="(^d{5}$)|(^d{5}-d{4}$)|^([ABCEGHJKLMNPRSTVXY]d[ABCEGHJKLMNPRSTVWXYZ])+{0,1}(d[ABCEGHJKLMNPRSTVWXYZ]d)$"> </div>
            </div>
            <div class="clear"></div>
        </div>
        <div id="iff-rollup52230" class="iff-roleup"> </div>
    </div>
</div>

我需要提取文本并单击每个div的单选按钮。

//div[contains(text(),'" +textvalue+ "')]/preceding:://input[@value='Yes'])

1 个答案:

答案 0 :(得分:0)

尝试以下:

//for yes radio button
driver.findElement(By.cssSelector("div.iff-optin label:nth-child(1) input")).click();

//for No radio button
driver.findElement(By.cssSelector("div.iff-optin label:nth-child(2) input")).click();

<强>更新

如果您只想为所有无线电检查是,请按照以下代码进行操作:

 List<WebElement> camp2 = driver.findElements(By.cssSelector("div.iff-optin label:nth-child(1) input"));

    System.out.println(camp2.size());

    for(WebElement ele :camp2){
        ele.click();


    }