选择标签后面的单选按钮

时间:2017-12-11 01:12:28

标签: python python-3.x selenium selenium-webdriver

我在选择一个特定的单选按钮(男性或女性)时遇到了麻烦我看过其他几个帖子都无济于事。我收到错误消息:

selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见   (会话信息:chrome = 62.0.3202.94)   (驱动程序信息:chromedriver = 2.33.506120(e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform = Windows NT 10.0.16299 x86_64)

HTML:

<div class="btn-group gender-input form-field customize" data-toggle="buttons" title="Gender">
    <label for="census_primary_genderMALE" class="btn btn-input "><input type="radio" value="MALE" name="census.primary.gender" id="census_primary_genderMALE"><div class="radio-switcher"></div>Male</label>

    <label for="census_primary_genderFEMALE" class="btn btn-input active"><input type="radio" checked="checked" value="FEMALE" name="census.primary.gender" id="census_primary_genderFEMALE"><div class="radio-switcher"></div>Female</label>
</div>

My Code

3 个答案:

答案 0 :(得分:0)

  • 点击 MALE

    driver.findElement(By.xpath("//input[@id='census_primary_genderMALE']")).click();
    
  • 点击 FEMALE

    driver.findElement(By.xpath("//input[@id='census_primary_genderFEMALE']")).click();
    

答案 1 :(得分:0)

您可以使用任何这些xpath

男性

//input[@id='census_primary_genderMALE' and @value='MALE']
//input[@type='radio' and @value='MALE']

这样的代码:

driver.findElement(By.xpath("//input[@type='radio' and @value='MALE']")).click();

女性

//input[@id='census_primary_genderFEMALE' and @value='FEMALE']
//input[@type='radio' and @value='FEMALE']

这样的代码:

driver.findElement(By.xpath("//input[@type='radio' and @value='FEMALE']")).click();

您还可以使用id作为定位器来选择男性和女性复选框,因为它们是唯一的

女性

driver.findElement(By.xpath("census_primary_genderFEMALE)).click();

男性

driver.findElement(By.xpath("census_primary_genderMALE)).click();

答案 2 :(得分:0)

您可以在xpath定位器下面使用以下代码:

  • 点击MALE:driver.find_element_by_xpath("//label[@for='census_primary_genderMALE']").click()

  • 点击女性:driver.find_element_by_xpath("//label[@for='census_primary_genderFEMALE']").click()