Selenium Webdriver - 如果没有id,如何选择复选框?

时间:2018-01-28 15:07:58

标签: java selenium-webdriver

所以我有这个Google Docs调查,有复选框,我想自动检查Selenium Webdriver。我试图找到元素ID,但它没有一个,只有一个类名。

<label class="docssharedWizToggleLabeledContainer freebirdFormviewerViewItemsRadioChoice">
    <div class="exportLabelWrapper">
        <div class="quantumWizTogglePaperradioEl docssharedWizToggleLabeledControl freebirdThemedRadio freebirdThemedRadioDarkerDisabled freebirdFormviewerViewItemsRadioControl" jscontroller="EcW08c" jsaction="click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseleave:JywGue; touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd; focus:AHmuwe; blur:O22p3e; keydown:I481le; contextmenu:mg9Pef" jsshadow="" aria-label="Männlich" tabindex="0" data-value="Männlich" aria-describedby="  i5" role="radio" aria-checked="false" aria-posinset="1" aria-setsize="3">
            <div class="quantumWizTogglePaperradioInk exportInk"></div>
            <div class="quantumWizTogglePaperradioInnerBox"></div>
            <div class="quantumWizTogglePaperradioRadioContainer">
                <div class="quantumWizTogglePaperradioOffRadio exportOuterCircle">
                    <div class="quantumWizTogglePaperradioOnRadio exportInnerCircle"></div>
                </div>
            </div>
        </div>
        <div class="docssharedWizToggleLabeledContent">
            <div class="docssharedWizToggleLabeledPrimaryText">
                <span dir="auto" class="docssharedWizToggleLabeledLabelText exportLabel freebirdFormviewerViewItemsRadioLabel">Männlich</span>
            </div>
        </div>
    </div>
</label>
<div class="quantumWizTogglePaperradioRadioContainer">
    <div class="quantumWizTogglePaperradioOffRadio exportOuterCircle">
        <div class="quantumWizTogglePaperradioOnRadio exportInnerCircle"></div>
    </div>
</div>

“Männlich”是标签的文本,属于我想要标记的复选框,但不是复选框本身的元素。

我试过了

driver.findElement(By.name("name"))
driver.findElement(By.id("id"))

在这种情况下。

1 个答案:

答案 0 :(得分:0)

您必须使用xpath:https://www.w3schools.com/xml/xpath_syntax.asp

driver.findElement(By.xpath("//div[@class='docssharedWizToggleLabeledContent']/div"))之类的东西应该有用。