用硒选择单选按钮值

时间:2019-02-25 01:20:36

标签: python-3.x selenium-webdriver

我有一个包含源代码的页面,如下面的示例。该页面上有一个单选按钮,其中有5个值可供选择。我正在尝试选中“第一价值”框。我尝试了以下所有尝试,但都没有碰到任何运气。

我也尝试了类似文章中的所有内容: Using Selenium in Python to click/select a radio button

但没有一个对我有用。

有人知道这个问题是什么,可以提出解决方案的建议吗?

来源:

<div class="col-sm-11">
  <div class="radio">
    <label><input type="radio" name="questionaire3" value="Value One">Value One </label>
  </div>
</div>

代码:

# driver.find_element_by_css_selector("[input type='radio'][value='Value One']").click()
# driver.find_element_by_css_selector("input[type='radio'][value='Value One']").click()
driver.find_element_by_css_selector("input[name='questionaire3'][value='Value One']").click()

错误:

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
<ipython-input-60-e1d0a5c5435d> in <module>()
      7 # driver.find_element_by_css_selector("input[type='radio'][value='Value One']").click()
      8 
----> 9 driver.find_element_by_css_selector("input[name='questionaire3'][value='Value One']").click()

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in find_element_by_css_selector(self, css_selector)
    595             element = driver.find_element_by_css_selector('#foo')
    596         """
--> 597         return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
    598 
    599     def find_elements_by_css_selector(self, css_selector):

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in find_element(self, by, value)
    964         return self.execute(Command.FIND_ELEMENT, {
    965             'using': by,
--> 966             'value': value})['value']
    967 
    968     def find_elements(self, by=By.ID, value=None):

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    318         response = self.command_executor.execute(driver_command, params)
    319         if response:
--> 320             self.error_handler.check_response(response)
    321             response['value'] = self._unwrap_value(
    322                 response.get('value', None))

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"input[name='questionaire3'][value='Value One']"}
  (Session info: chrome=72.0.3626.109)
  (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)

更新:

这有效

driver.find_element_by_name(“ questionaire3”)。send_keys('Value One')

1 个答案:

答案 0 :(得分:0)

这有效:

driver.find_element_by_name("questionaire3").send_keys('Value One')